简体   繁体   中英

How to convert a string with zeros to number in javascript?

I have this string: "140.000.000" , when I try to convert it to number with parseInt or parseFloat , it always give me 140 without the rest, I already tried with replacing the "." with "," , but it does not work, someone can help me?

"140.000.000" isn't a valid Number, neither "140,000,000" . Run the code below,

 console.log(isNaN("140.000.000")) console.log(isNaN("140,000,000")) console.log(isNaN("140000000"))

However, "140000000" is. So, try replacing the . with empty string as,

 str = "140.000.000" n = Number(str.replace(/\./g, '')) console.log(n)

 const str ="140.000.000" console.log(parseInt(str.split('.').join('')))

Remove the dots:

 const num = parseInt(`140.000.000`.replace(/\./gm, '')); console.log(num)

Try the following

 const string = "140.000.000"; const number = parseInt(string.replace(/\./g,"")); console.log(number);

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