This question already has an answer here:
Keep trailing or leading zeroes on number
(1 answer)
Closed 9 months ago.
I have some string values like "35.5" , "32.20" and I want them to be converted to numbers but keep the exact same decimals. When I use Number("32.0") for example I get 32 but I want 32.0. If I convert Number("35.5") I want 35.5 not 35.50, is there any way to do this easily?
if you want a fixed number of floating point you can use toFixed but be aware that returns a string
const strings = [ "35.5" , "32.20", "32.0"]
const result = strings.map(n => parseFloat(n).toFixed(1))
console.log(result)
Related
This question already has answers here:
BigDecimal in JavaScript
(5 answers)
Why max digits with decimal in JavaScript are only 16
(2 answers)
Closed 4 months ago.
i am working on a Web3 project where i have a balance of tokens which i want to burn. The problem that the amount of balance is in this format
0.29806008728157019
So when i pass this amount to the burn method it loss its precision and it becomes
0.2980600872815702
I've tried many things like converting it to big number before passing it as a parameter like this
web3.utils.toBN( Math.trunc(amount * 1000000000000000000) );
i tried to convert it to a string then use parseFloat like this:
const value = web3.utils.toBN(
Math.trunc(parseFloat(amount) * 1000000000000000000)
);
But none of these worked.
Any help please ?
This question already has answers here:
How do you convert numbers between different bases in JavaScript?
(21 answers)
Closed 5 months ago.
I have some numbers (base 10) that I want to convert to a 32 bits(base 2), I have tried a lot of things, I found out the >>> operator, but apparently it only converts negative numbers to a base 10 the equivalent of the 32 bits, instead of base 2
const number = 3
const bitNumber = 1 >>> 0
console.log(bitNumber) /// 1
The numbers are always stored as bits internally. It’s console.log that converts them to a string, and the string conversion uses decimal by default.
You can pass a base to Number.prototype.toString:
console.log(bitNumber.toString(2));
and display as many bit positions as you want:
console.log(bitNumber.toString(2).padStart(32, '0'));
This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
Closed 11 months ago.
I tried to format the Amount field to 2 digits using the
Amount.toLocaleString()
toLocaleString(undefined, { minimumFractionDigits: 2 }),
Some of the Amount values are displayed correctly but few are incorrect
like $52.85.00 should be displayed as $52.85
function financial(x) {
return Number.parseFloat(x).toFixed(2);
}
console.log(financial(123.456));
console.log(financial(0.004));`
You can use Number.prototype.toFixed() to format with a number of decimals. Note: The toFixed method will return a string, so you should convert back to an floating number if your goal is not just to display. For more info on .toFixed() method access document
let numberToBeFormatted = 212.121212;
let formattedNumber = parseFloat(numberToBeFormatted.toFixed(2))
This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 5 years ago.
I have large decimal numbers which I am getting from a request & I want to convert them to string.
So for EG:
I tried all methods converting to string
var r=12311241412412.1241523523523235
r.toString();
r+'';
''+r;
String(r);
//output
'12311241412412.1241'
//what i want
'12311241412412.1241523523523235'
All methods return the decimal numbers upto 4 digits (12311241412412.1241)
but i want all the number till end.
I also tried r.toFixed().toString() but each time the length of decimal numbers change.
What would be easy way to do this?
the problem is that 12311241412412.1241523523523235 in javascript means 12311241412412.125. whatever you do is not gonna work unless you put the whole thing in a string at the first place.
use this instead:
var r = "12311241412412.1241523523523235";
This question already has answers here:
Is there a JavaScript function that can pad a string to get to a determined length?
(43 answers)
Closed 6 years ago.
Take this IP address:
192.168.1.1
I want to break it down into 4 full binary octets, so:
11000000 . 10101000 . 00000001 . 00000001.
All of the conversions I know, and those that I've found on other stackoverflow questions, only return the binary number itself, for example:
(1 >>> 0).toString(2) returns 1 when I want 00000001
Number(2).toString(2) returns 10 when I want 00000010
Is there an in-built javascript method that I haven't come across yet or do I need to manually add the 0's before depending on the number?
You can use Number#toString method with radix 2 for converting a Number to corresponding binary String.
var str = '192.168.1.1';
console.log(
// Split strings based on delimiter .
str.split('.')
// iterate over them
.map(function(v) {
// parse the String and convert the number to corresponding
// binary string afterwards add preceding 0's
// with help of slice method
return ('00000000' + Number(v).toString(2)).slice(-8)
// rejoin the string
}).join('.')
)
The simplest solution is to prefix the answer with zeros and then use a negative substring value to count from the right, not the left...
alert(("00000000" + (1 >>> 0).toString(2)).substr(-8));