This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am trying to find the value of the 4th decimal place and check if it is a zero or not. If not I was planning on throwing an error message. For example 2.3189. I need to be able to check the value of the 4th decimal which in this case is 9. This is the code I have thus far. It seemed to be working for majority of cases but for example 1.2570. When I do the check for this number it says that the 0 is not a 0. When I do the same check with 1.2580 it says that the 0 is a 0. Any help with this would be greatly appreciated.
!!(submission && (quantity * 10000 % 10) === 0);
I'd do regex checking 4th place after '.' to be zero
var number = 0.554156;
/\.[0-9]{3}0/.test(number.toString())
You could make use of javascript expression for accessing the 4th decimal place since modulus operator has precision issues.
const quantity = 1.2570;
console.log(Number(quantity.toFixed(4).split('.')[1][3]));
Related
This question already has answers here:
How to deal with floating point number precision in JavaScript?
(47 answers)
Closed 1 year ago.
I hope theres a javascript expert that can help.
I am trying to round numbers to 2 decimal places
example
3.342 = 3.34
4.565 = 4.57
means from the third digit is 5 and above to round to the next decimal, and with no change below 5
I have something like
var num = Math.round(n*100)/100
and it works for other numbers but not for
n = 4.225 which rounded would be 4.23
and because I couldn't find the issue I printed
n*100 this is
4.225*100 and it turns I got 422.49999999999994 instead of 422.5
and that explains why Math.round gives me 4.22 instead of 4.23.
please, can anybody give some light on this issue?
This is happening with other numbers too and I have no solution yet.
I also added math.Epsilon like
(n+math.Epsion) * 100
You can use Math.round combined with toFixed to solve your problem:
console.log(Math.round((4.225*100).toFixed(1)) / 100)
This question already has answers here:
Why parseFloat in javascript returns string type for me?
(2 answers)
Closed 4 years ago.
In my JS, I'm doing a check to see if one number is greater than the other. I'm attaching the image of Chrome DevTools:
As you can see here, the code has made it inside the if statement. On the right, in the Watch, you can see amount = "3.00" and available = "261.60".
What would cause the smaller number to be greater than the bigger number?
You are are comparing strings. In the code you are calling parseFloat, which is the right idea, but it's followed by toFixed(). toFixed() returns a string.
console.log(typeof parseFloat("3.00").toFixed(2))
You need to make sure you're comparing numbers. An easy way is:
if(+amount > +available)
Alternatively, don't call toFixed() until it's time to display the number.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I'm trying to get the number of digits of a number in javascript, but I'm running into some edge cases with the equations I've found online.
Here's what I'm using, from this site.
getNumDigits(val){
return val === 0 ? 1 : Math.floor(Math.log(Math.abs(val)) / Math.LN10 + 1);
}
But the problem with this is, if you put in a number like 1000, you somehow get a javascript rounding error where the value comes out like 3.999997
I've noticed that, as long as your number isn't between -1 and 1, you can just add 1 to the val in the Math.abs(val) and it will appropriately set the number of digits, but it just seems messy.
I've tried just converting the val into a string, and getting the length, but that doesn't work in the case of, say, a decimal, where you're using something like 0.2 - as it will say the length is 3.
What's a good equation / function to use?
Making the number a string and using a regex to only count the digits can work.
function getNumDigits(val) {
return (`${val}`.match(/\d/g) || []).length
}
console.log(getNumDigits(2.5245234))
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.
This question already has an answer here:
What is the standard solution in JavaScript for handling big numbers (BigNum)?
(1 answer)
Closed 6 years ago.
n = "11004691915666669"
parseInt(n)
parseInt(n, 10)
Both results are: 11004691915666668
This is off by one. I tested some other values and they convert correctly. Is this a bug in the library or is there a better way to convert?
See this SO question for more details. Your value is beyond the maximum integer that Javascript can represent with full accuracy.
Under the hood Javascript doesn't understand integers, everything's floating point. You're hitting the reduced accuracy that happens when you get to very large numbers.
From the Javascript reference at MDN given here.
The integer range for a Number is defined as follows :
var biggestInt = 9007199254740992;
var smallestInt = -9007199254740992;
Which is why since your number exceeds the biggestInt value preset by Javascript, the parseInt method only returns the integer value that lies within the Integer range for a Number in Javascript as defined above.