如何使用javascript截断(不舍入)小数点后的特定数字

时间:2021-05-16 17:07:12

input : -81.82637799999999(14 numbers after decimal point)

输入:-81.82637799999999(小数点后14位数)

output :-81.8263779999999(13 numbers after decimal point)

输出:-81.8263779999999(小数点后13位数)

How can I implement this using javascript?

如何使用javascript实现这一点?

1 个解决方案

#1


0  

Well try this. See if it works for you

好吧试试吧。看看它是否适合你

var x = -81.82637799999999;
x = x.toString(); // convert to string
alert(x.length);
x = x.substring(0, x.length - 1); // cut out last character
alert(x.length);
x = parseFloat(x); // convert it back to float

https://jsfiddle.net/9sngtp3n/1/

#1


0  

Well try this. See if it works for you

好吧试试吧。看看它是否适合你

var x = -81.82637799999999;
x = x.toString(); // convert to string
alert(x.length);
x = x.substring(0, x.length - 1); // cut out last character
alert(x.length);
x = parseFloat(x); // convert it back to float

https://jsfiddle.net/9sngtp3n/1/