Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Showing with 51 additions and 16 deletions.
  1. +24 −0 bower.json
  2. +26 −15 index.js
  3. +1 −1 package.json
View
24 bower.json
@@ -0,0 +1,24 @@
+{
+ "name": "number-abbreviate",
+ "main": "index.js",
+ "version": "0.0.2",
+ "homepage": "https://github.com/domharrington/js-number-abbreviate",
+ "description": "Abbreviate a number and add unit letters e.g. 2200000 => '2.2m'",
+ "keywords": [
+ "number",
+ "number-abbreviate",
+ "abbreviate"
+ ],
+ "authors": [
+ "Dom Harrington <dom@harrington-mail.com>"
+ ],
+ "license": "BSD-2-Clause",
+ "ignore": [
+ "**/.*",
+ "node_modules",
+ "bower_components",
+ "test",
+ "tests",
+ "test.js"
+ ]
+}
View
41 index.js
@@ -1,25 +1,36 @@
-module.exports = function numberAbbreviate(number, decPlaces) {
- decPlaces = Math.pow(10, decPlaces)
+(function(root){
+ 'use strict';
- var abbrev = [ 'k', 'm', 'b', 't' ]
+ function numberAbbreviate(number, decPlaces) {
+ decPlaces = Math.pow(10, decPlaces)
- for (var i = abbrev.length - 1; i>=0; i--) {
+ var abbrev = [ 'k', 'm', 'b', 't' ]
- var size = Math.pow(10, (i + 1) * 3)
+ for (var i = abbrev.length - 1; i>=0; i--) {
- if(size <= number) {
- number = Math.round(number * decPlaces / size) / decPlaces
+ var size = Math.pow(10, (i + 1) * 3)
- if((number === 1000) && (i < abbrev.length - 1)) {
- number = 1
- i++
- }
+ if (size <= number) {
+ number = Math.round(number * decPlaces / size) / decPlaces
+
+ if ((number === 1000) && (i < abbrev.length - 1)) {
+ number = 1
+ i++
+ }
- number += abbrev[i]
+ number += abbrev[i]
- break
+ break
+ }
}
+
+ return number
+ }
+
+ if (typeof module !== 'undefined' && module.exports) {
+ module.exports = numberAbbreviate;
+ } else {
+ root.numberAbbreviate = numberAbbreviate;
}
- return number
-}
+})(this);
View
2 package.json
@@ -1,6 +1,6 @@
{
"name": "number-abbreviate",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "Abbreviate a number and add unit letters e.g. 2200000 => '2.2m'",
"author": "Dom Harrington <dom@harrington-mail.com>",
"main": "index.js",

No commit comments for this range

Something went wrong with that request. Please try again.