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
-
14:54
The Kevin Trudeau Show Limitless
3 days agoThe Hidden Force Running Your Life
104K23 -
2:16:35
DLDAfterDark
9 hours ago $7.98 earnedIs The "SnapPocalypse" A Real Concern? Are You Prepared For SHTF? What Are Some Considerations?
25.1K10 -
19:58
TampaAerialMedia
20 hours ago $8.87 earnedKEY LARGO - Florida Keys Part 1 - Snorkeling, Restaurants,
41.6K17 -
1:23
Memology 101
2 days ago $7.93 earnedFar-left ghoul wants conservatives DEAD, warns Dems to get on board or THEY ARE NEXT
33.2K66 -
3:27:27
SavageJayGatsby
10 hours ago🔥🌶️ Spicy Saturday – BITE Edition! 🌶️🔥
58.9K7 -
26:09
Exploring With Nug
20 hours ago $12.44 earned13 Cold Cases in New Orleans What We Discovered Beneath the Surface!
54.4K26 -
27:39
MYLUNCHBREAK CHANNEL PAGE
15 hours agoDestroying Time.
137K41 -
3:27:19
Mally_Mouse
10 hours ago🌶️ 🥵Spicy BITE Saturday!! 🥵🌶️- Let's Play: Minecraft Christmas Adventure!!
135K12 -
2:14:31
Side Scrollers Podcast
15 hours agoSide Scrollers INVITE ONLY - Live From Dreamhack
161K16 -
1:18:23
Simply Bitcoin
2 days ago $14.64 earnedThe Bitcoin Crucible w/ Alex Stanczyk and Lawrence Lepard
39.7K7