October 03 2017

Javascript display money format

Tagged Under :

javascript
Sometimes we need using javascript to display money format in our system but javascript don’t have this kind of function. So we need to write a function to convert number to money format.

function moneyFormat(price, sign = 'MYR') {
	const pieces = parseFloat(price).toFixed(2).split('')
	let ii = pieces.length - 3
	while ((ii-=3) > 0) {
		pieces.splice(ii, 0, ',')
	}
	return sign + pieces.join('')
}
To using the function:
console.log(moneyFormat(10000.00))
from the above method it will display MYR 10,000.00.

Or you may using this library accounting.js to solve the problem.

Make a Comment

*

code