Blog gw

membuat format uang di javascript

Posted by dillah2008 on July 22, 2009

Sayang sekali javascript belum menyediakan fungsi untuk format uang seperti fungsi number_format() pada PHP. Akhirnya dapat fungsi untuk mengakali ini semua,

function add_commas(nStr){
nStr += ”;
x = nStr.split(‘,’);
x1 = x[0];
x2 = x.length > 1 ? ‘,’ + x[1] : ”;
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, ‘$1′ + ‘.’ + ‘$2′);
}
return x1 + x2;
}

Semoga bermanfaat

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>