Premium Only Content
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
-
17:12
World2Briggs
20 hours ago $0.07 earnedTop 10 Towns You Can Retire or Live on $1900 a month in the Midwest #1
33.2K24 -
17:25
BlackDiamondGunsandGear
1 day agoCustom Building the Cheapest MP5
41.1K1 -
2:07:20
BEK TV
1 day agoTrent Loos in the Morning - 11/06/2025
39.4K1 -
8:10
The Shannon Joy Show
19 hours agoShould we even VOTE anymore?
38.2K45 -
59:34
Dialogue works
1 day ago $0.03 earnedMohammad Marandi: It’s WAR: Iran’s Supreme Defense Council ACTIVATES –Hezbollah REFUSES to Surrender
71.3K14 -
10:23
TheSaltyCracker
20 hours agoMuslims Immediately Threaten New Yorkers After Zohran Win
70.8K490 -
18:40
Actual Justice Warrior
19 hours agoMamdani Pledges To DESTROY New York
48.8K105 -
28:53
iCkEdMeL
19 hours ago $0.08 earnedBREAKING: 9 DEAD After UPS Plane BURSTS Into Fireball at Louisville Airport
45.6K8 -
20:52
Professor Nez
22 hours agoThe TRUTH is Actually WORSE than we Thought...
32K34 -
8:59
MattMorseTV
21 hours ago $0.10 earnedTrump’s DIRE WARNING to the Senate GOP.
87.9K93