+ 1

Hello! Please help me! I need to replace format numbers from "1000000" to "10 000 000" in output with JS. How it works?

19th Jun 2019, 2:20 PM
Artem Kuksinov
Artem Kuksinov - avatar
3 Réponses
+ 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
20th Jun 2019, 2:10 AM
Daniel C
Daniel C - avatar
+ 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
19th Jun 2019, 3:20 PM
Calviղ
Calviղ - avatar
+ 2
Thanks!
19th Jun 2019, 5:25 PM
Artem Kuksinov
Artem Kuksinov - avatar