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);
Related
This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 1 year ago.
I had a problem with extracting numbers from strings. With all the inputs the code works correctly, but there is a task -
If there are two separate numbers in the string - return the first of them.
So from this string 'only 5 or 6 apples', I should get 5. Not 56 or 5 6. I have no idea what to do.
My code looks like this:
function count(apples) {
const number = Math.floor(Number(apples.replace(/[^0-9.]+/g, '')));
console.log(number);
}
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?
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
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"
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