Created
April 19, 2022 00:04
-
-
Save paulohenriquesn/f560877f80b1c4a4d96665ef26cc20ca to your computer and use it in GitHub Desktop.
This file contains 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
/* eslint-disable @typescript-eslint/ban-ts-comment */ | |
function number_format(number, decimals, decPoint, thousandsSep) { | |
// eslint-disable-line camelcase | |
number = (number + '').replace(/[^0-9+\-Ee.]/g, ''); | |
const n = !isFinite(+number) ? 0 : +number; | |
const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals); | |
const sep = typeof thousandsSep === 'undefined' ? ',' : thousandsSep; | |
const dec = typeof decPoint === 'undefined' ? '.' : decPoint; | |
let s = ''; | |
const toFixedFix = function (n, prec) { | |
if (('' + n).indexOf('e') === -1) { | |
//@ts-ignore | |
return +(Math.round(n + 'e+' + prec) + 'e-' + prec); | |
} else { | |
const arr = ('' + n).split('e'); | |
let sig = ''; | |
if (+arr[1] + prec > 0) { | |
sig = '+'; | |
} | |
return ( | |
(+( | |
//@ts-ignore | |
(Math.round(+arr[0] + 'e' + sig + (+arr[1] + prec)) + 'e-' + prec) | |
)).toFixed(prec) | |
); | |
} | |
}; | |
// @todo: for IE parseFloat(0.55).toFixed(0) = 0; | |
//@ts-ignore | |
s = (prec ? toFixedFix(n, prec).toString() : '' + Math.round(n)).split('.'); | |
if (s[0].length > 3) { | |
//@ts-ignore | |
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); | |
} | |
if ((s[1] || '').length < prec) { | |
//@ts-ignore | |
s[1] = s[1] || ''; | |
//@ts-ignore | |
s[1] += new Array(prec - s[1].length + 1).join('0'); | |
} | |
//@ts-ignore | |
return s.join(dec); | |
} | |
export function formatCurrency(value, currency) { | |
let result = 0.0; | |
if ( | |
['bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw', 'vnd'].includes(currency) | |
) { | |
result = number_format(Math.ceil(value), 0, '', ''); | |
} else { | |
result = number_format(value * 100, 0, '', ''); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment