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:06:09
								Inverted World Live
10 hours agoMysterious Crash at Area 51 | Ep. 134
31K13 - 	
				
							2:48:59
								TimcastIRL
8 hours agoTrump Endorses Cuomo, Says NO COMMIE MAMDANI, Obama REFUSES To Endorse Mamdani | Timcast IRL
265K179 - 	
				
							5:51:16
								Drew Hernandez
1 day agoGOP CIVIL WAR: TUCKER CARLSON DERANGEMENT SYNDROME AT ALL TIME HIGH
52.6K41 - 	
				
							14:44
								Sponsored By Jesus Podcast
1 day agoYou Can't Serve God & MONEY | Is Money the Root of All Evil?
23.3K20 - 	
				
							2:47:28
								Barry Cunningham
12 hours agoYOU'VE BEEN MISINFORMED! GREED IS ACTUALLY GOOD! ESPECIALLY NOW! (AND MORE NEWS)
100K42 - 	
				
							7:18:24
								SpartakusLIVE
11 hours agoSNIPING in Battlefield 6 - REDSEC || Monday MOTIVATION to CONQUER the Week
57.1K6 - 	
				
							49:25
								ThisIsDeLaCruz
7 hours ago $20.74 earnedBack Stage Pass with Avenged Sevenfold
61.4K10 - 	
				
							6:43:40
								GritsGG
13 hours agoWorld Record Win Streak Attempt! #1 Most Wins 3880+!
33.7K3 - 	
				
							3:12:05
								Tundra Tactical
10 hours ago $28.35 earnedProfessional Gun Nerd Plays Battlefield 6
61.7K6 - 	
				
							1:01:12
								Donald Trump Jr.
12 hours agoThe China Matrix with Journalist Lee Smith | TRIGGERED Ep.288
150K93