/**
*
* @param num : number need formatting
* @param n : to fixed ( number digit behind point number format), default = 2
*/
export function toFixedNoRounding(num, n = 2) {
const reg = new RegExp('^-?\\d+(?:\\.\\d{0,' + n + '})?', 'g');
const a = num.toString().match(reg)[0];
const dot = a.indexOf('.');
if (dot === -1) {
// integer, insert decimal dot and pad up zeros
return a + '.' + '0'.repeat(n);
}
const b = n - (a.length - dot) + 1;
return b > 0 ? a + '0'.repeat(b) : a;
}
Created
April 1, 2020 07:44
-
-
Save hungdev/08c59a350ca70a73989f5aea3d66f4f1 to your computer and use it in GitHub Desktop.
toFixedNoRounding
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment