How to Round Numbers into a Reader Friendly Format? (E.G. $1.1K)

Is there a way to round numbers into a reader friendly format? (e.g. $1.1k)

Here is a simple function to do it:

function abbrNum(number, decPlaces) {
// 2 decimal places => 100, 3 => 1000, etc
decPlaces = Math.pow(10,decPlaces);

// Enumerate number abbreviations
var abbrev = [ "k", "m", "b", "t" ];

// Go through the array backwards, so we do the largest first
for (var i=abbrev.length-1; i>=0; i--) {

// Convert array index to "1000", "1000000", etc
var size = Math.pow(10,(i+1)*3);

// If the number is bigger or equal do the abbreviation
if(size <= number) {
// Here, we multiply by decPlaces, round, and then divide by decPlaces.
// This gives us nice rounding to a particular decimal place.
number = Math.round(number*decPlaces/size)/decPlaces;

// Handle special case where we round up to the next abbreviation
if((number == 1000) && (i < abbrev.length - 1)) {
number = 1;
i++;
}

// Add the letter for the abbreviation
number += abbrev[i];

// We are done... stop
break;
}
}

return number;
}

Outputs:

abbrNum(12 , 1)          => 12
abbrNum(0 , 2) => 0
abbrNum(1234 , 0) => 1k
abbrNum(34567 , 2) => 34.57k
abbrNum(918395 , 1) => 918.4k
abbrNum(2134124 , 2) => 2.13m
abbrNum(47475782130 , 2) => 47.48b

Demo: http://jsfiddle.net/jtbowden/SbqKL/

iOS convert large numbers to smaller format

Here are two methods I have come up with that work together to produce the desired effect. This will also automatically round up. This will also specify how many numbers total will be visible by passing the int dec.

Also, in the float to string method, you can change the @"%.1f" to @"%.2f", @"%.3f", etc to tell it how many visible decimals to show after the decimal point.

For Example:

52935 ---> 53K
52724 ---> 53.7K

-(NSString *)abbreviateNumber:(int)num withDecimal:(int)dec {

NSString *abbrevNum;
float number = (float)num;

NSArray *abbrev = @[@"K", @"M", @"B"];

for (int i = abbrev.count - 1; i >= 0; i--) {

// Convert array index to "1000", "1000000", etc
int size = pow(10,(i+1)*3);

if(size <= number) {
// Here, we multiply by decPlaces, round, and then divide by decPlaces.
// This gives us nice rounding to a particular decimal place.
number = round(number*dec/size)/dec;

NSString *numberString = [self floatToString:number];

// Add the letter for the abbreviation
abbrevNum = [NSString stringWithFormat:@"%@%@", numberString, [abbrev objectAtIndex:i]];

NSLog(@"%@", abbrevNum);

}

}

return abbrevNum;
}

- (NSString *) floatToString:(float) val {

NSString *ret = [NSString stringWithFormat:@"%.1f", val];
unichar c = [ret characterAtIndex:[ret length] - 1];

while (c == 48 || c == 46) { // 0 or .
ret = [ret substringToIndex:[ret length] - 1];
c = [ret characterAtIndex:[ret length] - 1];
}

return ret;
}

Hope this helps anyone else out who needs it!

Need to convert number to currency

Just simplify calculation of number of digits:

// From:
var nbDigits = parseInt(Math.log(number)/Math.LN10);

// To:
var nbDigits1 = Math.log10(number);

That'll give you the number of digits, without rounding errors. It does return $1.00 K. for 1000.

Hope this helps!

1000000 to 1M and 1000 to 1K and so on in JS

Just for fun, check out Code Golf: Friendly Number Abbreviator

The shortest JS answer:

function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}

p.s. this is probably not the fastest or best solution.

Duplicate: Is there a way to round numbers into a reader friendly format? (e.g. $1.1k)

Abbreviate a localized number in JavaScript for thousands (1k) and millions (1m)

function intlFormat(num)
{
return new Intl.NumberFormat().format(Math.round(num*10)/10);
}
function makeFriendly(num)
{
if(num >= 1000000)
return intlFormat(num/1000000)+'M';
if(num >= 1000)
return intlFormat(num/1000)+'k';
return intlFormat(num);
}

Yields:

makeFriendly(1234567)
"1.2M"
makeFriendly(123457)
"123.5k"
makeFriendly(1237)
"1.2k"
makeFriendly(127)
"127"

Intl is the Javascript standard 'package' for implemented internationalized behaviours. Intl.NumberFormatter is specifically the localized number formatter. So this code actually respects your locally configured thousands and decimal separators.

Code-Golf: Friendly Number Abbreviator

J, 61 63 65 characters

((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.)

Output:

((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 1500 0
┌─┬─┐
│2│k│
└─┴─┘

((j.&(1&{)":({.%&1e3{:));{&' kMGTPE'@{.)(([:<.1e3^.{.),{:,{.) 987654321987654321 4
┌────────┬─┐
│987.6543│P│
└────────┴─┘

(The reason the output is "boxed" like that is because J doesn't support a list consisting of varying types)

Explanation (from right to left):

(([:<.1000^.{.),{:,{.)

We make a new 3-element list, using , to join ([:<.1000^.{.) (the floored <. base 1000 log ^. of the first param {.. We join it with the second param {: and then the first param {..

So after the first bit, we've transformed say 12345 2 into 1 2 12345

((j.&(1&{)":({.%&1000{:));{&' kMGTPE'@{.) uses ; to join the two halves of the expression together in a box to produce the final output.

The first half is ((j.&(1&{)":({.%&1000{:)) which divides (%) the last input number ({:) by 1000, the first number of times. Then it sets the precision ": using the second number in the input list (1&{).

The second half {&' kMGTPE'@{. - this uses the first number to select ({) the appropriate character from the 0-indexed list of abbreviations.



Related Topics



Leave a reply



Submit