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
-
7:51:03
GuardianRUBY
11 hours agoRumble Takeover! The Rumblings are strong
94.3K5 -
4:28:45
Etheraeon
18 hours agoWorld of Warcraft: Classic | Fresh Level 1 Druid | 500 Follower Goal
63.4K1 -
3:17:21
VapinGamers
10 hours ago $3.90 earned🎮🔥Scrollin’ and Trollin’: ESO Adventures Unleashed!
43.5K2 -
10:48:40
a12cat34dog
11 hours agoGETTING AFTERLIFE UNLOCKED :: Call of Duty: Black Ops 6 :: ZOMBIES CAMO GRIND w/Bubba {18+}
36.6K2 -
8:23:18
NubesALot
14 hours ago $5.66 earnedDark Souls Remastered and party games
35.5K -
3:03:42
GamersErr0r
1 day ago $2.36 earnedits not what you think
28.6K1 -
7:15:50
Phyxicx
12 hours agoRocket League with Friends! - 11/22/2024
21.8K1 -
7:54:29
STARM1X16
12 hours agoFriday Night Fortnite
18.1K1 -
29:51
Afshin Rattansi's Going Underground
1 day agoJimmy Dore on Ukraine & WW3: Biden Wants a War that Trump CAN’T Stop, ONLY Hope is Putin’s Restraint
76K35 -
3:20:54
Fresh and Fit
14 hours agoExposing WHO Killed JFK w/ Cory Hughes & Tommy Sotomayor
97.2K66