简体   繁体   English

如何使用 replace() 添加 '.' 数字之间?

[英]How to use replace() as adding '.' between the numbers?

Using Javascript, if my number is 5899, I want to insert a decimal point between the numbers to get the result of 58.99.使用Javascript,如果我的数字是5899,我想在数字之间插入一个小数点得到58.99的结果。 I know (num/100).toFixed(2) would work, but just wondering if there is a way for using replace.我知道(num/100).toFixed(2)会起作用,但只是想知道是否有使用替换的方法。

A regular expression can look ahead for 2 digits followed by the end of the string...正则表达式可以向前看 2 位数字,然后是字符串的结尾......

 console.log( String(5899).replace( /(?=\d{2}$)/, '.' ) );

but .toFixed makes a lot more intuitive sense, and so should probably be preferred.但是.toFixed更直观,因此可能应该是首选。

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

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