Web Programming: Number Format (php, Javascript)

Wuihh,.. agak ribet juga ngakalin supaya tampilan number format uang ke database (int) dan tampilin ke client.

Contoh: di database 100000 dan di client: 100,000

di client: (javascript) – hasil nyari2 di mbah google

<script type=”text/javascript”>
function calculate2(objA)
{ setFormat(objA);
var x_price = document.fproductedit.x_price.value.replace(/,/g,”);
//var dp = document.loandata.dp.value.replace(/,/g,”);
//if (!(isNaN(otr) && isNaN(dp)))
//document.loandata.principal.value = formatThous(Number(otr) – Number(dp));

}
function round(x) { return Math.round(x*100)/100;}

formatThous.rejectSign = true;
formatThous.stripSign = true;

function formatThous(num){
var f = formatThous;
if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split(‘.’).length > 2)
{
return num;
}

var l, c = 1, t = ”,
sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : ”;
num = num.toString(10).replace(/[^\d\.]/g, ”).split(‘.’);
l = num[0].length-1;
while (l + 1){
t = c%3||!l? num[0].charAt(l) + t : ‘,’ + num[0].charAt(l) + t;
l–;
c++;
}
return sign + t + (num[1] && num[1].length? ‘.’ + num[1] : ”);
}

function setFormat(objA)
{
if (objA!=”)
{ var a = eval(‘document.fproductedit.’ + objA);
a.value = formatThous(a.value);
}
}

document.fproductedit.x_price.focus();

</script>

dengan form-nya:

<form name=”fproductedit” id=”fproductedit” action=”productedit.php” method=”post” enctype=”multipart/form-data”>

dan

Rp<input type=”text” style=”text-align: right;” onkeyup=”calculate2(this.name);” size=”12″ name=”x_price” id=”x_price” value=”<?php echo number_format($x_price, 0,’,’,’.’);?>”>

Jadi tampilan pada saat input-ing dan tarik dari database itu selalu dibubuhi koma, koma. :-bd

kemudian storing ke database-nya:

$theValue = str_replace(“,”, “”, $theValue);
$fieldList[“`harga`”] = $theValue;

Mudah2an bermanfaat.

2 thoughts on “Web Programming: Number Format (php, Javascript)

    • coba di share disini langkah2nya seperti apa dan hasilnya seperti apa. Sebab tulisan itu ialah hasil praktek saya sendiri, walau mungkin terjadi ialah adanya salah ketik. ditunggu.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s