Float sum with javascript [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
I'm calculating the sum of several float values using javascript and... I've noticed a strange thing never seen before. Executing this code:
parseFloat('2.3') + parseFloat('2.4')
I obtain 4.699999999999999
So... what sould I do to obtain a correct value? (supposed that this is incorrect...)

Once you read what What Every Computer Scientist Should Know About Floating-Point Arithmetic you could use the .toFixed() function:
var result = parseFloat('2.3') + parseFloat('2.4');
alert(result.toFixed(2));​

(parseFloat('2.3') + parseFloat('2.4')).toFixed(1);
its going to give you solution i suppose

Related

Javascript simple multiply gives wrong result [duplicate]

This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Dealing with float precision in Javascript [duplicate]
(5 answers)
Closed 5 years ago.
I just want for example multiply 7.50*1.19
Iam pretty sure it´s 8,925 but if i calculate it in my js i get 8.924999999999999 and i cannot get a correct rounding to finally get 8,93
parseFloat(tarif.field_grundpreis * 1.19)
Result = 8.924999999999999
While tarif.field_grundpreis is 7.50
It´s crazy, please help.

JavaScript Console doesn't have correct value [duplicate]

This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 7 years ago.
I was goofing around when I decided to try to show a varaible on the console. this is my JS
test = 8888888888888888888;
console.log(test)
however, on the console, this was shown:
Why did 8888888888888888888 go to 8888888888888889000?
The number is larger than the largest value that can be represented exactly in a double-precision floating point value. Modern runtimes expose a constant on the Number constructor with the maximum value (Number.MAX_SAFE_INTEGER). The value is 9007199254740991.

Error on Javascript on Multiplying Decimals 16.08 * 100 [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 7 years ago.
Why the result of this data is doesn't give the exact value expected by a human brain or expected output of a person to appear
16.08 * 100 = 1607.9999999999998;
When i tried it to the console developer of the chrome browser;
and i tried using
console.log(16.08*100); gives 1607.9999999999998
Supposedly the answer that i should be seeing is
16.08 * 100 = 1608;
will you please help me with these. or any explanations
use .toFixed(n) method of javascript.
console.log((16.08 * 100).toFixed());

Javascript strange result of the sum [duplicate]

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 8 years ago.
My application works fine, but sometimes it return "strange" result, I dont't understand why it happens. For ex:
var a = 80.78, b = 83.49, c = 45.33
alert (a+b+c); //209.59999999999997 instead of 209.60
Thanks
A.

Show Eur results with two decimal places [duplicate]

This question already has answers here:
How to format numbers as currency strings
(67 answers)
Closed 9 years ago.
I am new to JavaScript so would appreciate help on this issue I have.
I have a booking form I have made and I have 3 Eur results that come up automatically following the selection choices made (#Martina helped me with this challenge).
What I would like to do is get the results to show with 2 decimal places only - here is the link to the page I am talking about:
https://www.alpinemalta.net/oilandgaslibya/bookNow.html
I've tried looking through some of the posts in this forum and attempted some of the things but my newness to JS has not helped :(
Thanks again in advance for any help on this.
Cheers - Chris Brown
The Number class in Javascript has a .toFixed method that does exactly what you require, e.g.:
var n = 234.1
var s = n.toFixed(2); // s = "234.10"

Categories