คำนวณแล้ว ต้องการใส่ ทศนิยม 2 ตำแหน่งให้กับตัวเลขครับ
Code (JavaScript)
function addCommas(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;
}
JavaScript Number Format and Comma
Date :
2017-06-20 17:42:07
By :
mr.win
Date :
2017-06-20 19:16:20
By :
mr.win
อ้างอิงจาก getElementById ง่ายกว่า
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<script language="javascript">
function addCommas(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;
}
function fncCal()
{
var tot = 0;
for(i=1;i<=document.getElementById("count").value;i++)
{
tot=(parseFloat(document.getElementById("txtVol1_"+i).value) * parseFloat(document.getElementById("txtVol2_"+i).value)).toFixed(2);;
document.getElementById("txtVol3_"+i).value=addCommas(tot);
}
}
</script>
<form action="page.cgi" method="post" name="form1">
Input 1 <input name="txtVol[]" id="txtVol1_1" type="text"> <input name="txtVol[]" id="txtVol2_1" type="text"> = <input name="txtVol[]" id="txtVol3_1" type="text"><br>
Input 2 <input name="txtVol[]" id="txtVol1_2" type="text"> <input name="txtVol[]" id="txtVol2_2" type="text"> = <input name="txtVol[]" id="txtVol3_2" type="text"><br>
Input 3 <input name="txtVol[]" id="txtVol1_3" type="text"> <input name="txtVol[]" id="txtVol2_3" type="text"> = <input name="txtVol[]" id="txtVol3_3" type="text"><br>
Input 4 <input name="txtVol[]" id="txtVol1_4" type="text"> <input name="txtVol[]" id="txtVol2_4" type="text"> = <input name="txtVol[]" id="txtVol3_4" type="text"><br>
Input 5 <input name="txtVol[]" id="txtVol1_5" type="text"> <input name="txtVol[]" id="txtVol2_5" type="text"> = <input name="txtVol[]" id="txtVol3_5" type="text"><br>
<input type="hidden" id="count" name="hdnLine" value="5">
<input name="btnCal" type="button" value="Cal" OnClick="JavaScript:fncCal();">
</form>
</body>
</html>
ประวัติการแก้ไข 2017-06-21 13:24:21
Date :
2017-06-21 13:22:36
By :
dudesaranyu
Date :
2017-06-21 18:02:26
By :
mr.win
Load balance : Server 05