+ 1
Hello! Please help me! I need to replace format numbers from "1000000" to "10 000 000" in output with JS. How it works?
3 Answers
+ 4
In addition to the accepted response here, JavaScript provides a function to do this for you. If you have a number, you can use .toLocaleString() to separate the number with commas where necessary. For example:
var n = 10000000;
var output = n.toLocaleString();
alert(output); // Alerts 10,000,000
If you prefer the commas be replaced with spaces, you can use:
output = output.replace(/,/g, " ");
alert(output); // Alerts 10 000 000
+ 3
var n = 10000000;
var out = n.toString().split("").reverse().map((v,i,a) => (i<a.length-1 && i%3==0) ? v + " ": v).reverse().join("");
alert(out)
https://code.sololearn.com/WT4eWic9QqwK/?ref=app
+ 2
Thanks!