简体   繁体   中英

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. 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

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 :

 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.

 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()小数点右边的所有内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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