Skip to content

Instantly share code, notes, and snippets.

@chinthaka-b
chinthaka-b / numberformat.js
Created March 15, 2016 05:26
number_format(number, decimals, decPoint, thousandsSep) in JavaScript, known from PHP. It formats a number to a string with grouped thousands, with custom seperator and custom decimal point
function number_format(number, decimals, decPoint, thousandsSep){
decimals = decimals || 0;
number = parseFloat(number);
if(!decPoint || !thousandsSep){
decPoint = '.';
thousandsSep = ',';
}
var roundedNumber = Math.round( Math.abs( number ) * ('1e' + decimals) ) + '';