Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Forked from timgit/megaNumber.js
Created November 7, 2015 12:17
Show Gist options
  • Save juanpujol/5115478872aa0a10d611 to your computer and use it in GitHub Desktop.
Save juanpujol/5115478872aa0a10d611 to your computer and use it in GitHub Desktop.
Large number format filter for Angular written in ES6 that rounds to the specified decimal place (defaults to 1). 1 billion => 1B, 1,490,000 => 1.5M, 999,999 => 1M
angular.module('utilsModule').filter("megaNumber", () => {
return (number, fractionSize) => {
if(number === null) return null;
if(number === 0) return "0";
if(!fractionSize || fractionSize < 0)
fractionSize = 1;
var abs = Math.abs(number);
var rounder = Math.pow(10,fractionSize);
var isNegative = number < 0;
var key = '';
var powers = [
{key: "Q", value: Math.pow(10,15)},
{key: "T", value: Math.pow(10,12)},
{key: "B", value: Math.pow(10,9)},
{key: "M", value: Math.pow(10,6)},
{key: "K", value: 1000}
];
for(var i = 0; i < powers.length; i++) {
var reduced = abs / powers[i].value;
reduced = Math.round(reduced * rounder) / rounder;
if(reduced >= 1){
abs = reduced;
key = powers[i].key;
break;
}
}
return (isNegative ? '-' : '') + abs + key;
};
});
describe('megaNumber tests', function(){
var $filter, megaNumberFilter;
beforeEach(module('utilsModule'));
beforeEach(inject(function(_$filter_){
$filter = _$filter_;
megaNumberFilter = $filter('megaNumber');
}));
it('1,500,000,000 is converted to 1.5B', function(){
const value = 1.5 * Math.pow(10,9);
var result = megaNumberFilter(value);
expect(result).toBe("1.5B");
});
it('1,490,000,000 is rounded to 1.5B', function(){
const value = 1.49 * Math.pow(10,9);
var result = megaNumberFilter(value);
expect(result).toBe("1.5B");
});
it('1,550,000,000 is rounded to 1.6B', function(){
const value = 1.55 * Math.pow(10,9);
var result = megaNumberFilter(value);
expect(result).toBe("1.6B");
});
it('1,500,000 is converted to 1.5M', function(){
const value = 1.5 * Math.pow(10,6);
var result = megaNumberFilter(value);
expect(result).toBe("1.5M");
});
it('1,500 is converted to 1.5K', function(){
const value = 1.5 * Math.pow(10,3);
var result = megaNumberFilter(value);
expect(result).toBe("1.5K");
});
it('1,550 is rounded to 1.6K', function(){
const value = 1.55 * Math.pow(10,3);
var result = megaNumberFilter(value);
expect(result).toBe("1.6K");
});
it('1,555 is rounded to 1.56K with 2 decimal places', function(){
const value = 1555;
var result = megaNumberFilter(value, 2);
expect(result).toBe("1.56K");
});
it('999 is rounded to 1K', function(){
const value = 999;
var result = megaNumberFilter(value);
expect(result).toBe("1K");
});
it('990,000 is rounded to 1M', function(){
const value = 990000;
var result = megaNumberFilter(value);
expect(result).toBe("1M");
});
it('990,000 is not rounded to 1M if 2 decimal places', function(){
const value = 990000;
var result = megaNumberFilter(value, 2);
expect(result).toBe("990K");
});
it('-999 is rounded to -1K', function(){
const value = -999;
var result = megaNumberFilter(value);
expect(result).toBe("-1K");
});
it('0 is still 0', function(){
const value = 0;
var result = megaNumberFilter(value);
expect(result).toBe("0");
});
it('null is still null', function(){
var result = megaNumberFilter(null);
expect(result).toBe(null);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment