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))
Related
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]));
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:
Large numbers erroneously rounded in JavaScript
(6 answers)
Closed 1 year ago.
I'm sure this has something to do with floating point math or something, but I need to out how to correctly interpret this number 9703248444284653. Javascript apparently doesn't like it
If I just do this.
var test = 9703248444284653
console.log(test)
The result is 9703248444284652, which is obviously not correct. Problem is that 9703248444284653 is an id that can't be changed. I have no idea what to do with this.
Your value is larger than Number.MAX_SAFE_INTEGER.
console.log(9703248444284653n > Number.MAX_SAFE_INTEGER);
You should treat your IDs as BigInt values. So you can should probably store the values as strings and then parse them as BigInt values if you need to perform any math on them.
const
bigIntNative = 9703248444284653n,
bigIntConstructed = BigInt('9703248444284653');
console.log(typeof bigIntNative === 'bigint' && bigIntNative === bigIntConstructed);
console.log(`${bigIntNative}`);
This question already has answers here:
Truncate number to two decimal places without rounding
(43 answers)
Closed 5 years ago.
I'm trying to get the first decimals of a float number without any kind of rounding.
Example:
var myfloat = 1.1864526;
myfloat = myfloat.toFixed(2);
It returns 1.19 but I need 1.18.
I'm pretty sure there is an easy solution but I am unable to find it without converting the number to a string (not useful in this case).
Multiply the float value by 100, get the int value of the result then divide that int by 100. Something like this should work:
((int)(myFloat*100)) / 100
This question already has answers here:
Large numbers erroneously rounded in JavaScript
(6 answers)
Javascript parseInt() with leading zeros
(7 answers)
Closed 7 years ago.
I'm trying to convert a string to number in Javascript, but here is my problem :
var string = "068999999501111443";
parseInt(string); // 68999999501111440
Number(string); // 68999999501111440
Why does this happen and how to fix it ?
Thanks
This is because the number is too large to be stored accurately. It is being stored as a floating point number, which can only store a certain amount of precision. Beyond it's maximum precision, you'll get what look like weird rounding errors.
You'll get similar effects for decimals with a large number of decimal places. This is more well known, as it tends to occur more often, but it's exactly the same effect that is happening here.