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:46
BIG NEM
6 hours agoDiscovering RAKIJA: The Holy Liquer of the Balkans
442 -
1:11:38
Film Threat
11 hours agoCHRISTMAS DAY CHILL STREAM WITH CHRIS GORE | Hollywood on the Rocks
123K24 -
14:22:40
The Quartering
1 day agoYule Log Christmas MAGA Edition With Memes! Come Hang Out!
213K29 -
38:41
MYLUNCHBREAK CHANNEL PAGE
1 day agoTimeline Begins in 1800? - Pt 1 & 2
94.6K49 -
1:23:41
Game On!
1 day ago $13.01 earnedNetflix NFL Christmas Games Preview and Predictions!
84.9K13 -
2:05:07
Darkhorse Podcast
1 day agoWhy Trump Wants Greenland: The 257th Evolutionary Lens with Bret Weinstein and Heather Heying
313K726 -
8:50:58
Right Side Broadcasting Network
1 day ago🎅 LIVE: Tracking Santa on Christmas Eve 2024 NORAD Santa Tracker 🎅
412K62 -
2:48
Steven Crowder
1 day agoCROWDER CLASSICS: What’s This? | Nightmare Before Kwanzaa (Nightmare Before Christmas Parody)
361K13 -
33:49
Quite Frankly
1 day agoThe Christmas Eve Midnight Telethon
148K30 -
2:12:46
Price of Reason
1 day agoAmber Heard BACKS Blake Lively Lawsuit Against Justin Baldoni! Is Disney CEO Bob Iger in TROUBLE?
92.4K27