In this video, we are going to learn how to add commas to large numbers. In English, we use commas to group the digits of large numbers into threes. This practice drastically improves readability. In JavaScript, however, integers are stored and processed with no commas regardless of how small or big they are. For this reason, we have to create a custom function that adds commas to the numbers stored in JavaScript variables before displaying them on an HTML page.
e.g.
JavaScript: let nPages = 1083;
HTML: 1,083 pages
JavaScript: let price = 12985;
HTML: £12,985
JavaScript: let nViews = 287530;
HTML: 287,530 views
JavaScript: let nSubscribers = 2470937;
HTML: 2,470,937 subscribers
JavaScript: let population = 20189201;
HTML: a population of 20,189,201
JavaScript: let currentDistanceToMars = 157921823;
HTML: 157,914,823 km
JavaScript: let GDP = 20809217620725;
HTML: $20,541,317,620,725
We will name this function addCommas(). The parameter of the function is a whole number greater than or equal to 1,000. And its output is the same number but with commas added. When the number is passed to the function, the number is an integer, but when it is outputted with commas added, the number is no longer an integer but a string. To be completely sure that the function really works, I tested the function in a very rigorous way. Specifically, I used an array of large and very large numbers, randomly generated., e.g.
1000
8928
77614
846008
5242414
92333111
593437737
2304220622
73346433504
995652222448
1551159724126
47939888725917
The function also comes with its own error handlers that scrutinise the input received by the function. The error messages are personalised and can be found in the console of the browser.
Further links
PHP function that adds commas to large numbers: • PHP function that adds commas to large num...
/* *** *** *** *** *** *** *** */
If you want to watch videos about mathematics and physics, you can subscribe to my second YouTube channel: https://www.youtube.com/@Sohcahtoa160....
You can support my work by becoming a patron: / sohcahtoa1609
Follow Sohcahtoa1609 on Twitter: / sohcahtoa1609
Like Sohcahtoa1609 on Facebook: / sohcahtoa1609
Follow Sohcahtoa1609 on Instagram: / sohcahtoa1609
Disclaimer: this video is meant to be a proof of concept.
Nesta página do site você pode assistir ao vídeo on-line JavaScript function that adds commas to large numbers | Number.isInteger(), Math.floor(), slice() duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário PHP scripts 2020 28 Maio 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 1,693 vezes e gostou 19 espectadores. Boa visualização!