Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making large numbers readable in JavaScript

I'm wondering if there is a way to make large numbers readable in JavaScript. I'm sure there is I just can't find it. For example, if I am writing

for (var i=0; i < 1000000; i++){
codecodecode};

is there a way to write that 1000000 so that it's readable without disrupting the for loop?

Furthermore, is there a way of returning a large number so that, too, is readable?

Sorry if I explained this poorly, I'm just starting out...

Thanks in advance!

like image 857
Kelly Hall Avatar asked Aug 29 '26 05:08

Kelly Hall


2 Answers

Add underscore

100_000_000_000

const loopCount = 50_000

for (var i=0; i < 1_000_000; i++){ codecodecode};

for more information go here (https://2ality.com/2018/02/numeric-separators.html)

like image 128
Omer Avatar answered Aug 31 '26 20:08

Omer


If you are thinking about the source code, you can write 1E6. You are looking for some symbol to seperate the thousands, but unfortunately there is no way.

If you want to convert a number to a more readable string, then this SO post may help you.

like image 36
apartridge Avatar answered Aug 31 '26 20:08

apartridge