Compact representation of a large number in JavaScript ( #shorts )
Hi Friends,
Compact representation of a large number in JavaScript
To generate a compact representation of a large number in JavaScript, you can use the following function
function getCompactNumber(numbr) {
const units = ['', 'K', 'M', 'B', 'T'];
let unitIndex = 0;
while (numbr ≥ 1000 && unitIndex < units.length - 1) {
numbr /= 1000;
unitIndex++;
}
return numbr.toFixed(1).replace(/\.0$/, '') + units[unitIndex];
}
getCompactNumber(1000); // "1K"
getCompactNumber(1200); // "1.2K"
getCompactNumber(1200000); // "1.2M"
getCompactNumber(1200000000); // "1.2B"
getCompactNumber function takes a number as an argument and returns a string representing the number in a compact format. The function uses an array of units to check which unit to use for the compact representation, and then divides the number by 1000 until it is less than 1000 or the largest unit has been used. The number which will be returned as result, is then formatted to one decimal place and any trailing ".0" is removed.
#javascript #compact #number #compactnumber #large #largenumber #string #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #famous #coders #development #code #dev #interview #php #trend #answer #youtube #instagram #socialmedia #education #edit #gaming #how #india #knowledge #love #like #motivational #quiz #reels #short #subscribe #share #update #video #ytshorts #youtuber #princecheema_official @princecheema_official
-
0:59
Pawanvir Singh Cheema
1 year agoJavaScript convert An Object into An Array ( #shorts )
22 -
0:44
Minute Math
2 years agoHow Many Squares Do You See? | Minute Math #shorts
21 -
0:39
Carolina Capital Management
10 months agoThe Importance Of Breaking Down Your KPIs To Smaller Numbers #shorts
-
0:10
Pawanvir Singh Cheema
1 year agoJavaScript String functions trimStart() & trimEnd() #javascript #shorts
1 -
0:34
Pawanvir Singh Cheema
1 year agoDifferent ways to Sum Array Values in JavaScript (#shorts )
26 -
1:07:38
Anointing Oil Dynamics
7 months agoCompressing Multiple Signs
6 -
0:50
Amara
1 year agoThe Algorithms Are Changing!! #shorts
9 -
4:16
hayksimonyanrumble
8 months agoLeetCode 155 - Min Stack (JavaScript)
2 -
0:36
Minute Math
2 years agoBad Math That Works | Simplify √3/10 | Part 11 | Minute Math #shorts
12 -
1:38
Legalrice
2 years agoAccurate Representation Of Numi
2