Convert string to float without precision loss javascript [duplicate] - javascript

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 ?

Related

What's the deal with big number faulty arithematic in JS? [duplicate]

This question already has answers here:
What is JavaScript's highest integer value that a number can go to without losing precision?
(21 answers)
Large numbers erroneously rounded in JavaScript
(6 answers)
javascript large integer round because precision? (why?)
(2 answers)
Closed 1 year ago.
So, I found something I couldn't understand and can't find any internet resource that explains it.
Please see the code below:
var num = 35422484817926290
// subtract 5 from num
console.log(num-5)
Output (Wrong) : 35422484817926284
I checked it in Node, Opera, and Chrome, all of them give the wrong answers.
I do understand the fact that arithmetic with unsafe Integers in JS is faulty, for example:
console.log(100000000000000005-1)
Output (Wrong) : 100000000000000000
So what's the deal with big number arithmetic in JS?
When I run this code:
var num = 35422484817926290
// subtract 5 from num
console.log(num-5)
in Visual Studio Code, i get the following warning:
"Numeric literals with absolute values equal to 2^53 or greater are too large to be represented accurately as integers."
So the correct way to make this calculation would be like this:
var num = 35422484817926290n
// subtract 5 from num
console.log(num-5n)
JavaScript is not faulty, this is how the Floating point arithmetic works. Looks duplicate to this post. For better calculation involving floating-point numbers you should use BigNumber API.

Round off Decimal Value in HTML [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Format number to always show 2 decimal places
(37 answers)
Closed 1 year ago.
I take data from Spreadsheet, So I use the below formula in App script
Now the data in spreadsheet value=2.33344443333
Hence in HTML same 2.33344443333 it's showing when i call emailTemp.data, Please help me to change to have only 2 decimal value like 2.33
Appscript:
data.forEach(function(row){
emailTemp.data=row[value];
}
.toFixed(2) would round a decimal value to two spaces as a string. Presumably the following would work if you didn't need to access the value as a decimal later on:
data.forEach(function(row){
emailTemp.data=row[value].toFixed(2);
}
Try this :
data.forEach(function(row){
emailTemp.data = Math.round(Number(row[value]) * 100) / 100;
}

Convert Large Decimal Number to String in Javascript [duplicate]

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";

Javascript - simple division leads to error [duplicate]

This question already has answers here:
How to deal with big numbers in javascript [duplicate]
(3 answers)
Closed 8 years ago.
If I use the windows calculator to calculate
(1.75 + 3/1.75)/2 it yields to =
1,7321428571428571428571428571429
If I do the same with javascript it yields to
1.732142857142857206298458550009
So at position 22 after the decimal point the result becomes incorrect ...142857... vs. ...206298...
var a = 1.75;
var res = (a+3/a)/2;
console.log(res.toFixed(30));
How can I make my division precise for 31 digits after the decimal comma?
Javascript can't do that "per se", since its double variables have a limited precision. You'll need to use an external library to handle operations with big precision numbers, like this one: Javascript Bignum

Show 0 value in floating point values, for currency [duplicate]

This question already has answers here:
Format number to always show 2 decimal places
(37 answers)
Closed 8 years ago.
How can I make a flaoting point number always have two decimal places, even when one is a zero?
E.g var postageCost = 3.2 but I want to display it as 3.20 - is this something that must be added as a string or is it possible to actually change the number?
From Here, you can use
parseFloat(Math.round(postageCost * 100) / 100).toFixed(2);

Categories