Javascript : round float number issue [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 7 years ago.
I have a problem with rounding float number. Here the source code, very simple :
a = "2.3";
result = parseFloat(a)+0.01
console.log(result);
Console displays 2.3099999999999996 instead of 2.31. You can try here : https://jsfiddle.net/fh9bj83u/
Have you a solution ?
Thank you in advance, cordially

Use toFixed to trim to 2 decimal places.
result = (parseFloat(a)+0.01).toFixed(2)
"2.31"

Related

What is the solution to avoid floating point precision issue in javascript? [duplicate]

This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I am having a problem when you perform an equation similar to this one:
let result = (num1/num2) - 1;
An example for that one is (157.23 / 120) -1, javascript gives a result of 31.024999999 but a regular calculator or calculator application give a result of FIXED 31.025
Is there any way how to avoid this issue?

How to add fraction and whole number number in javascript? [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
for example in javascript addition of 1 + 0.59 is 1.5899999999999999 but I want output of 1.59 which looks right to me.
Use .toFixed() to round your number.
alert((1+0.58999999999).toFixed(2));

javascript math result lesser than 0 gets 0.0000000000 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why does javascript returns so many zeros and not just 0.24
And how can i disable javascript to do this. Because when im using a calculator i never get the result 0.24000000000002
var sum = (0.0001 * 2400);
result 0.2400000000002
The reason for this is that your sum is a float which are known to not be very precise. This is a limitation of float values.
To fix this you need to round the decimals by either Math.round or .toFixed.
javascript always do that but you can make it show only 2 digits after the dot.
var sum = (0.0001 * 2400);
alert(sum.toFixed(2));

strange Javascript Float division result [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
var coeff='0.1';
var amount='12.2';
var res = Math.floor(parseFloat(amount) / parseFloat(coeff));
console.log(res);
Why the result of this is 121 (I was expecting 122)?
EDIT: my question was ambiguous: no trouble with the floor function. I was just wondering why 12.2 / 0.1 is not equal to 122.
the result is 121.99999999999999
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
The Math.floor() function returns the largest integer less than or equal to a given number.
so it cuts off the .99999. you might want to use
Math.round(parseFloat(amount) / parseFloat(coeff));
instead

Possible Javascript numerical bug [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
This code snippet:
Math.pow(10,20)+20000 == Math.pow(10,20) + 10000
evaluates to true. Is this a JS bug?
No. Javascript uses floating point numbers to represent large values, and the value 1020 exceeds the precision of the floating point numbers. When you add a relatively tiny value such as 20000 or 10000 to the result, the resulting sums are indistinguishable.

Categories

Resources