@synotech/utils
Version:
a collection of utilities for internal use
28 lines (25 loc) • 631 B
text/typescript
import currency from 'currency.js';
/**
* This method validates email address
* @module currencyMask
* @param {number} number - a number to mask as currency
* @return {string} {String} a string of currency
* @example
*
* currencyMask(1000) // returns 1,000
*
*/
export const currencyMask = (number: any): string => {
number = number || 0;
number = currency(number || 0);
number += '';
const x = number.split('.');
let x1 = x[0];
const x2 = x.length > 1 ? '.' + x[1] : '';
const rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
const price = x1 + x2;
return price;
};