链接:https://blog.csdn.net/a772116804/article/details/125916129
假设1w 我们需要除以1亿 10010/100000000 ≈ 0.0001
- const decimalFn = (num) => {
- if (Number.isInteger(num)) {
- return num;
- }
- let num2 = num.toString()
- console.log(num2,'num2')
- const [interger, decimal] = `${num2}`.split('.');
- // console.log(decimal)
- const index = decimal.split('').findIndex(v => Number(v) > 0)
- return interger + '.'+ decimal.substring(0, index + 2) //1 代表有效位数1位,2的话就是2位,3就是三位
- }
- console.log(decimalFn(0.00058905))