format number javascript code example
Example 1: how to number format in javascript
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}
console.info(formatNumber(2665))
console.info(formatNumber(102665))
console.info(formatNumber(111102665))
console.info(formatNumber(1240.5))
console.info(formatNumber(1000240.5))
Example 2: js format urcurency
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
formatter.format(2500);
Example 3: javascript format price
const number = 123456.789;
console.log(new Intl.NumberFormat('ja-JP', {
style: 'currency',
currency: 'BWP',
}).format(number));
Example 4: float to currency js
function floatToEuro(float){
var euroCurrency
euroCurrency = '\u20AC' + float.toLocaleString('nl-NL',{minimumFractionDigits: 2});
console.log(euroCurrency);
return euroCurrency;
};
Example 5: javascript convert a number in string
const num = 123;
const str = num.toString();
Example 6: get the whole value of a number javascript
var value = 2.9802453587962963;
var wholeNum = Math.floor(value);
console.log(wholeNum);