简体   繁体   English

如何在 jquery 或 javascript 中获得精确的两位数

[英]how to get exact two digits after decimal point in jquery or javascript

I want the exact 2 digits after the decimal point.我想要小数点后的确切 2 位数字。 I tried the toFixed(2) function but it returns rounded off 2 digits.我尝试了toFixed(2) function 但它返回四舍五入的 2 位数字。 Here is my Code it returns 18.70 but I want 18.69 extract number这是我的代码它返回18.70但我想要18.69提取号

rim_weight = (23* 36 *70);
rim_weight = rim_weight/3100; // retuns 18.69677419354839
rim_weight = rim_weight.toFixed(2); // returns 18.70

If you just want to truncate the number to two decimal places, first divide by 31, convert to an integral value using Math.floor and then divide by 100:如果您只想将数字截断到小数点后两位,请先除以 31,使用Math.floor转换为整数值,然后除以 100:

 rim_weight = (23 * 36 * 70); rim_weight = Math.floor(rim_weight / 31); rim_weight = rim_weight / 100; console.log(rim_weight);

just convert it to string then match the number up to the second place只需将其转换为字符串,然后将数字匹配到第二位

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM