create simplified toFixedIfLarger

This commit is contained in:
Daniel Ternyak 2017-06-24 14:53:53 -05:00
parent 119dda77e1
commit d5875614de
1 changed files with 1 additions and 23 deletions

View File

@ -1,27 +1,5 @@
//flow
// https://stackoverflow.com/questions/9539513/is-there-a-reliable-way-in-javascript-to-obtain-the-number-of-decimal-places-of
const decimalPlaces = (function() {
function isInt(n) {
return (
typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n)
);
}
return function(n) {
let a = Math.abs(n);
let c = a,
count = 1;
while (!isInt(c) && isFinite(c)) {
c = a * Math.pow(10, count++);
}
return count - 1;
};
})();
export function toFixedIfLarger(number: number, fixedSize: number = 6): string {
if (decimalPlaces(number) > fixedSize) {
return number.toFixed(fixedSize);
} else {
return String(number);
}
return parseFloat(number.toFixed(fixedSize)).toString();
}