approximate-number
Advanced tools
Comparing version 2.0.0 to 2.1.0
module.exports = { | ||
"env": { | ||
"browser": true, | ||
"node": true, | ||
"amd": true | ||
'env': { | ||
'browser': true, | ||
'node': true, | ||
'amd': true | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
'extends': 'eslint:recommended', | ||
'rules': { | ||
'indent': [ | ||
'error', | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
'linebreak-style': [ | ||
'error', | ||
'unix' | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
'quotes': [ | ||
'error', | ||
'single' | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
'semi': [ | ||
'error', | ||
'always' | ||
] | ||
} | ||
}; |
@@ -42,3 +42,7 @@ /* Approximate Number - outputs numbers in human-readable format, similar to ls -lh or Stack Overflow's reputation | ||
} | ||
num = workingNum < 10 ? (Math[ROUND](workingNum * 10) / 10) : Math[ROUND](workingNum); | ||
if (opts.precision) { | ||
num = workingNum; | ||
} else { | ||
num = workingNum < 10 ? (Math[ROUND](workingNum * 10) / 10) : Math[ROUND](workingNum); | ||
} | ||
num = num.toString(); | ||
@@ -67,2 +71,4 @@ if (typeof opts.decimal === 'string') { | ||
* @param {String} [opts.suffix=''] Optional string to append to the value, e.g. '%' | ||
* @param {Boolean} [opts.capital=false] Set to true to use capital letters, e.g. 3.9M instead of 3.9m | ||
* @param {Number} [opts.precision] Optional number of significant digits. Must be greater than 0. | ||
* | ||
@@ -81,2 +87,6 @@ * @returns {String} | ||
if (opts.precision) { | ||
num = parseFloat(num.toPrecision(opts.precision)); | ||
} | ||
var thousandsBreak = opts.min10k ? TEN_THOUSAND : THOUSAND; | ||
@@ -86,8 +96,10 @@ | ||
numString = addCommas(formatDec(num, 1, opts), opts); | ||
} else if (opts.precision && opts.precision > Math.log10(num)) { | ||
numString = addCommas(formatDec(num, 1, opts), opts); | ||
} else if (num < MILLION) { | ||
numString = formatDec(num, THOUSAND, opts) + 'k'; | ||
numString = formatDec(num, THOUSAND, opts) + 'k'; | ||
} else if (num < BILLION) { | ||
numString = formatDec(num, MILLION, opts) + 'm'; | ||
numString = formatDec(num, MILLION, opts) + 'm'; | ||
} else if (num < TRILLION) { | ||
numString = addCommas(formatDec(num, BILLION, opts), opts) + 'b'; | ||
numString = addCommas(formatDec(num, BILLION, opts), opts) + 'b'; | ||
} else { | ||
@@ -94,0 +106,0 @@ numString = addCommas(formatDec(num, TRILLION, opts), opts) + 't'; |
{ | ||
"name": "approximate-number", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "lib/approximate-number.js", | ||
@@ -47,4 +47,4 @@ "description": "Converts numbers into a more human-friendly format, similar to `ls`'s `--human-readable` flag (`ls -lh`) or Stack Overflow's reputation numbers. For example, 123456 becomes '123k'.", | ||
"devDependencies": { | ||
"eslint": "^3.12.1", | ||
"mocha": "^3.2.0" | ||
"eslint": "^7.26.0", | ||
"mocha": "^8.4.0" | ||
}, | ||
@@ -56,6 +56,4 @@ "scripts": { | ||
"lint": "eslint .", | ||
"autofix": "eslint . --fix", | ||
"prepublish": "npm test", | ||
"postpublish": "git push && git push --tags" | ||
"autofix": "eslint . --fix" | ||
} | ||
} |
@@ -23,6 +23,12 @@ # Approximate Number [![Build Status](https://secure.travis-ci.org/nfriedly/approximate-number.png?branch=master)](http://travis-ci.org/nfriedly/approximate-number) | ||
console.log('My Stack Overflow reputation is %s.', approx(3671, {min10k: true})); | ||
console.log('My Stack Overflow reputation is %s.', approx(3671, { | ||
min10k: true | ||
})); | ||
//> My Stack Overflow reputation is 3,671. | ||
console.log('The US national debt is %s.', approx(19939034457936, {prefix: '$', capital: true, round: true})); | ||
console.log('The US national debt is %s.', approx(19939034457936, { | ||
prefix: '$', | ||
capital: true, | ||
round: true | ||
})); | ||
// > The US national debt is $20T. | ||
@@ -60,3 +66,3 @@ | ||
* **separator** {String|Boolean} Default `','`. Thousands separator - set to a string (e.g. '.') to use that string or false to not use any separator. | ||
* **separator** {String|Boolean} Default = `','`. Thousands separator - set to a string (e.g. '.') to use that string or false to not use any separator. | ||
* **decimal** {String|Boolean} Default = `'.'`. Decimal - set to a string (e.g. ',') to use that or set to false to avoid outputting values with a decimal. | ||
@@ -67,3 +73,8 @@ * **round** {Boolean} Default = `false`. Round numbers off rather than flooring/truncating. When true, 105000 would become '11m', when false it becomes '10m'. | ||
* **suffix** {String} Default = `''`. Optional string to append to the value, e.g. '%'. | ||
* **capital** {Boolean} Default = `false`. Set to true to use capital letters, e.g. 3.9M instead of 3.9m | ||
* **precision** {Number} Default = undefined. Number of significant digits. Must be greater than 0. Use of this option forces rounding. | ||
## V2.1 Changes | ||
* Added precision | ||
## V2 Changes | ||
@@ -70,0 +81,0 @@ |
9283
141
92
5