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
-
2:03:07
Badlands Media
1 day agoDevolution Power Hour Ep. 393: Synthetic Reality, and the RICO Grande Strategy
65.2K24 -
11:23:18
GritsGG
12 hours agoQuad Win Streaks!🫡 Most Wins in WORLD! 3600+
77.3K10 -
41:16
MattMorseTV
7 hours ago $77.29 earned🔴Antifa is PREPARING for WAR.🔴
74.7K431 -
2:55:38
cosmicvandenim
14 hours agoCOSMIC VAN DENIM | Horror Game | STOREBOUND
27.1K3 -
50:18
Side Scrollers Podcast
8 hours agoLIVE SPECIAL EVENT - Blabs SPITS on Her Ancestors by BURNING Pasta
50.1K8 -
6:18:07
putther
7 hours ago $2.26 earned⭐ Bounty Hunting on GTA Online & GTA 5 RP ❗
37.4K -
2:03:20
Joker Effect
5 hours agoYou Will Never Believe How Cooked Kick Streamers Are. Come Debate Me If You Think I am Wrong!
29.8K3 -
6:59:50
FrizzleMcDizzle
7 hours agoSilent Hill f - NEW HORROR GAME - COME CHAT
27.1K3 -
4:01:22
Mally_Mouse
6 days ago🌶️ 🥵Spicy BITE Saturday!! 🥵🌶️- Let's Play: Unrailed 2!
50.9K3 -
23:53
MYLUNCHBREAK CHANNEL PAGE
1 day agoIstanbul SHOULD NOT Exist - Pt 3
73.6K34