简体   繁体   中英

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. I know (num/100).toFixed(2) would work, but just wondering if there is a way for using replace.

A regular expression can look ahead for 2 digits followed by the end of the string...

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

but .toFixed makes a lot more intuitive sense, and so should probably be preferred.

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