This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 6 years ago.
I am trying to add two numbers in JavaScript, but the result is inexact. This my example:
var x = 11.12, y = 11.07;
console.log(x + y); // the result is 22.189999999999998 but the real result is 22.19
Any solution for this?
This is what you need to do:
var x = 11.12, y = 11.07;
var result = (parseFloat(x) + parseFloat(y)).toFixed(2);
console.log( result );
Related
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 2 years ago.
When I take the 5 root of a big number, js gives me a wrong answer
var pow = 5;
var A = 133;
var B = 110;
var C = 84;
var D = 27;
var E;
E = Math.pow((Math.pow(A, pow) + Math.pow(B, pow) + Math.pow(C, pow) + Math.pow(D, pow)), 1/pow);
console.log(E)
This should give 144 but gives 144.00000000000003
5 root of 61917364224 = 144
This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 3 years ago.
With numbers you can multiply a number a certain number of times using the following code:
var y = 10;
var x = y * 3; // 30
Is there a way to do the same with strings with native methods?
var y = "test ";
var x = y * 3; // test test test
Or if not is there a succinct way to do this in a single line?
You could do:
var y = "test ";
y = y.repeat(3);
And this would give you "test test test "
String.repeat()
What would be "string"*3 in Python, can be done with "string".repeat(3) in Javascript. That appears to be what you want.
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
JavaScript adding decimal numbers issue [duplicate]
(4 answers)
Closed 4 years ago.
I know JavaScript has a notorious problem with math that can cause math problems to return wrong with wonky decimal numbers. But how does one do a simple addition and subtraction in JS and not have a wonky result? And the numbers for the addition and subtraction are not fixed numbers and relay on variables. For example...
addition
var a = 3.94
var b = 0.5
var r = a + b
console.log(r) //4.4399999999999995
subtract
var a = 4.22
var b = 0.5
var r = a - b
console.log(r) //3.7199999999999998
I am aware of the math floating point issue in JS but I am looking for a simple solution to not have this issue occur when doing some addition and subtraction.
Is floating point math broken?
Thanks to #Stakvino suggestion I think I figured it out.
Let me know what you guys think.
addition
var a = 3.94
var b = 0.5
var fixed = String(a)
if (fixed.indexOf('.') === -1) {
fixed = 0;
}
else {
fixed = fixed.split('.')
fixed = fixed[1].length;
}
var r = (a + b).toFixed(fixed)
console.log(r)
subtraction
var a = 4.22
var b = 0.5
var fixed = String(a)
if (fixed.indexOf('.') === -1) {
fixed = 0;
}
else {
fixed = fixed.split('.')
fixed = fixed[1].length;
}
var r = (a - b).toFixed(fixed)
console.log(r)
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have an issue with the subtraction of two values. when I set discount_amt value to 2.5 then total return me 0.5 but when discount_amt is set to 2.6 it return 0.3999999999999999 instead of 0.4 why?
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
var total = parseFloat('3').toFixed(1);
var discount_amt = parseFloat('2.6').toFixed(1);
total = total - discount_amt;
console.log(total);
This seems to fix it. You forget parsefloat() and tofixed()
total = 3;
discount_amt = 2.6;
console.log(parseFloat(total).toFixed(1) + ' ' + parseFloat(discount_amt).toFixed(1));
total = parseFloat(total).toFixed(1) - parseFloat(discount_amt).toFixed(1);
console.log(parseFloat(total).toFixed(1));
Explanation why floats are handled this way: answer or directly to the link that answer refers to link
This question already has answers here:
Javascript roundoff number to nearest 0.5
(10 answers)
Closed 7 years ago.
I'm looking for an output of
4.33 = 4.5
4.5517263843648 = 5
Using the Math.round function in javascript. I've tried a few methods but neither seem to work.
Try to use,
function test(val) {
var x = Math.floor(val);
return (val - x) > 0.5 ? Math.ceil(val) : (x + 0.5);
}
DEMO