简体   繁体   English

从小数点后三位开始舍入

[英]Cut from the third decimal without rounding

I know it's quite low quality of question but I get little bit confused now. 我知道问题的质量很低,但是我现在有点困惑。 Here is the thing What I want to do. 这是我想做的事情。

100000 => 100.000
9997080000 => 9997080.000 

I want to cut from the third decimal without rounding. 我想从小数点后三位开始舍入而不舍入。 How can I do this? 我怎样才能做到这一点? I used the toFixed() but all I want to do is just cut from third decimal. 我使用了toFixed()但我要做的只是从小数点后三位开始。 I think I'm complicated now. 我想我现在很复杂。 It will be simple. 这很简单。 Plz let me know. 请让我知道。 Thanks 谢谢

From what I understand, this is what you want: 据我了解,这就是您想要的:

 var number = "9002764000"; var result = number.slice(0, -3) +"."+ number.slice(-3); console.log(result); 

This will add a . 这将添加一个. after the last three digits ie 9002764000 -> 9002764.000 最后三位数字之后,即9002764000 > 9002764.000

https://jsfiddle.net/5yqhr7mo/ https://jsfiddle.net/5yqhr7mo/

Hope it helps! 希望能帮助到你!

It sounds like you want a value for display . 听起来您想要显示一个值。 If so, you want to turn the number into a string (since numbers don't intrinsically have any particular number of digits to the right of the decimal). 如果是这样,则要将数字转换为字符串(因为数字本质上在小数点右边没有任何特定的数字位数)。 You can then easily insert the . 然后,您可以轻松插入. before the last three digits using substring and substr : 使用substringsubstr在最后三位数字之前:

 function formatForDisplay(num) { var str = String(num); return str.substring(0, str.length - 3) + "." + str.substr(-3); } function test(num) { console.log(num, "=>", formatForDisplay(num)); } test(100000); test(9997080000); 

Alternatively, you could use String#replace and add a dot. 另外,您可以使用String#replace并添加一个点。

 var number = 9002764000; console.log(Math.floor(number).toString().replace(/(?=...$)/, '.')); 

what about : 关于什么 :

 function formatNumber(num){ num=num/1000; return num.toFixed(3); } console.log(formatNumber(9997080000)); console.log(formatNumber(100000000)); 

which will return what you expect if you work with integer 如果使用整数,它将返回您期望的结果

Math.floor()将舍入到四舍五入,从而Math.floor()小数点右边的所有内容。

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

相关问题 如何从字符串中删除第三个符号并在没有第三个符号的情况下发出警报 - How to cut third symbol from string and alert it without third symbol 十进制比较,不舍入javascript - Decimal Comparison without rounding in javascript 用Javascript计算时不舍入十进制数 - Calculation in Javascript without rounding decimal numbers 在 Javascript 中将整数打印为小数而不四舍五入 - Printing a whole number as a decimal in Javascript without rounding 将数字截断到两位小数而不四舍五入 - Truncate number to two decimal places without rounding 如何显示小数点后一位(不四舍五入),而整数则显示.0? - How to show one decimal place (Without rounding) and show .0 for whole numbers? 如何在 javascript 中使用精确的十进制数格式化数字而不进行四舍五入? - How to format a number with exact of decimal number without rounding in javascript? 求和而不将其四舍五入到小数点后两位 - Getting a sum without rounding it off to two decimal places 在 JavaScript 中获得两个小数位而不四舍五入到下一个更大的数字 - Obtain two decimal places in JavaScript without rounding to the next bigger number 格式化数字不四舍五入,限制小数位,对数字进行进一步操作 - Format number without rounding, limitimg decimal places, operate futher on number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM