Created
October 28, 2013 18:31
-
-
Save Chocksy/7202086 to your computer and use it in GitHub Desktop.
This is a simple angularjs filter that makes big numbers into short format. 1 milion becomes 1m and 1122 becomes 1.1k
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app = angular.module 'myApp', [] | |
@app.filter "nrFormat", -> | |
(number) -> | |
if number!=undefined | |
console.log number | |
abs = Math.abs(number) | |
if abs >= Math.pow(10, 12) | |
# trillion | |
number = (number / Math.pow(10, 12)).toFixed(1)+"t" | |
else if abs < Math.pow(10, 12) and abs >= Math.pow(10, 9) | |
# billion | |
number = (number / Math.pow(10, 9)).toFixed(1)+"b" | |
else if abs < Math.pow(10, 9) and abs >= Math.pow(10, 6) | |
# million | |
number = (number / Math.pow(10, 6)).toFixed(1)+"m" | |
else if abs < Math.pow(10, 6) and abs >= Math.pow(10, 3) | |
# thousand | |
number = (number / Math.pow(10, 3)).toFixed(1)+"k" | |
number | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
thanks for inspiration, here is my version AND unit tests: