Javascript floating issue with addition [duplicate] - javascript

This question already has answers here:
Is floating point math broken?
(31 answers)
Float sum with javascript [duplicate]
(2 answers)
Closed 3 years ago.
I have following value which give wrong total.
let a = 86.2500;
let b = 32.3550;
alert(a+b); //return 118.60499999999999 , expected 118.605
alert((a+b).toFixed(2)) //return 118.60 , expected 118.61
When I calculate above value with my calculator it give my expected result but javascript give me unexpected result. Why and what is solution to get expected result?
https://jsfiddle.net/vnu9fyb8/1/

Try this to get the result:
let a = 86.2500;
let b = 32.3550;
alert((a+b).toFixed(3));
var digit = parseFloat((a+b).toFixed(3)).toFixed(2);
alert(digit);
I hope it will work for you.

toFixed(2) cannot make it become 118.61 because of the full result is 118.60499999999999 so "6049" in front of 0 is number 4. 4 can't be floored up to be 5, but if in front of 0 is number 5 it sure will be floored up to 118.61.
So this can't become 118.61.
You can refer to this link

Related

Javascript : Math.round is not working correctly for specific digits [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 5 years ago.
I am facing weird issue during rounding the value using javascript. Some of the value is not rounding in correct format.
var n =17.955 ;
var roundedPrice;
roundedPrice = Math.round(n*100)/100;
console.log(roundedPrice); // It returns 17.95 instead of 17.96
It is happening for some specific values like 16.955, 17.955, 18.955, 19.955. Except these values like 1.955, 12.955, 20.955, 27.955 ... This round function return correct values.
Edited : It is happening with 17.955 only. This returns correct result with 17.9555 ( 3 times 5).
Thanks in advance.
You can use either Math.ceil() to get the expected result.

Adding 2 numbers in javascript [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
While adding 2 numbers I am getting a lengthy decimal part
Pasting the code below.
Number(200.59) + Number(100) =
300.59000000000003
//Expected result 300.59
You could round the number to keep only 2 decimals:
var input = 300.59000000000003
var result = (Math.round(input*100)/100);
alert(result);

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

how to round a number to 2 decimal place? javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 7 years ago.
Im trying to round a number to 2 decimal place. I have tried the following but not having any luck? Can somebody please help me and tell me where im going wrong??
var winPercentage = totalWins/(totalWins+totalLost)*100;
winPercentage.toFixed(2);
document.getElementById('win-percentage').innerHTML = winPercentage + " %";
i search and tried this but to be honest i have no idea what it is?
var winPercentage = totalWins/(totalWins+totalLost)*100;
expr {double(round(100*winPercentage))/100}
document.getElementById('win-percentage').innerHTML = winPercentage + " %";
Try to use the following syntax instead and alter it to your needs
var num = 5.1;
num.toFixed(2); //will become 5.10
You had the right idea with toFixed(2). The problem is that it returns the formatted number, it does not alter the variable it was called on. In other words, you just need to assign it back the the variable you were using:
winPercentage = winPercentage.toFixed(2);

Categories

Resources