Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert long number into abbreviated string in JavaScript, with a special shortness requirement

In JavaScript, how would one write a function that converts a given [edit: positive integer] number (below 100 billion) into a 3-letter abbreviation -- where 0-9 and a-z/A-Z are counting as a letter, but the dot (as it's so tiny in many proportional fonts) would not, and would be ignored in terms of the letter limit?

This question is related to this helpful thread, but it's not the same; for instance, where that function would turn e.g. "123456 -> 1.23k" ("123.5k" being 5 letters) I am looking for something that does "123456 -> 0.1m" ("0[.]1m" being 3 letters). For instance, this would be the output of hoped function (left original, right ideal return value):

0                      "0" 12                    "12" 123                  "123" 1234                "1.2k" 12345                "12k" 123456              "0.1m" 1234567             "1.2m" 12345678             "12m" 123456789           "0.1b" 1234567899          "1.2b" 12345678999          "12b" 

Thanks!

Update: Thanks! An answer is in and works per the requirements when the following amendments are made:

function abbreviateNumber(value) {     var newValue = value;     if (value >= 1000) {         var suffixes = ["", "k", "m", "b","t"];         var suffixNum = Math.floor( (""+value).length/3 );         var shortValue = '';         for (var precision = 2; precision >= 1; precision--) {             shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));             var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');             if (dotLessShortValue.length <= 2) { break; }         }         if (shortValue % 1 != 0)  shortValue = shortValue.toFixed(1);         newValue = shortValue+suffixes[suffixNum];     }     return newValue; } 
like image 806
Philipp Lenssen Avatar asked May 15 '12 11:05

Philipp Lenssen


People also ask

How do you abbreviate string in JavaScript?

JavaScript Code: abbrev_name = function (str1) { var split_names = str1. trim(). split(" "); if (split_names. length > 1) { return (split_names[0] + " " + split_names[1].

How do you format numbers in JavaScript?

JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.


1 Answers

I believe ninjagecko's solution doesn't quite conform with the standard you wanted. The following function does:

function intToString (value) {     var suffixes = ["", "k", "m", "b","t"];     var suffixNum = Math.floor((""+value).length/3);     var shortValue = parseFloat((suffixNum != 0 ? (value / Math.pow(1000,suffixNum)) : value).toPrecision(2));     if (shortValue % 1 != 0) {         shortValue = shortValue.toFixed(1);     }     return shortValue+suffixes[suffixNum]; } 

For values greater than 99 trillion no letter will be added, which can be easily fixed by appending to the 'suffixes' array.

Edit by Philipp follows: With the following changes it fits with all requirements perfectly!

function abbreviateNumber(value) {     var newValue = value;     if (value >= 1000) {         var suffixes = ["", "k", "m", "b","t"];         var suffixNum = Math.floor( (""+value).length/3 );         var shortValue = '';         for (var precision = 2; precision >= 1; precision--) {             shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));             var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');             if (dotLessShortValue.length <= 2) { break; }         }         if (shortValue % 1 != 0)  shortValue = shortValue.toFixed(1);         newValue = shortValue+suffixes[suffixNum];     }     return newValue; } 
like image 99
chucktator Avatar answered Oct 11 '22 21:10

chucktator