2021年4月3日星期六

Count digits after decimal including zero javascript [duplicate]

I have this;

export const countDecimals = (value) => {    let num = parseFloat(value.replace(/,/g, ""));    if (Math.floor(num) === num) return 0;    return num.toString().split(".")[1].length || 0;  }  

it works until I enter a number with trailing zero, such as 2.10. It ignores the zero and returns 1.

How to fix this?

P.S. Reason I am doing parseFloat is because in my case value is string and will be formatted if it is a large number like "2,190.10".

https://stackoverflow.com/questions/66925564/count-digits-after-decimal-including-zero-javascript April 03, 2021 at 05:40AM

没有评论:

发表评论