0

Dear Experts

I have following codes, it works fine to sum. but when I enter these values
2100.49+100.20 then it says 2,200.6,899,999,999,996 insted of 2,200.69. How to overcome this problem. Please help

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">

<title>Numeric Textbox Sample</title>

<style type="text/css">
#box1 {width:200px;height:170px;border:1px solid green;background:#e3eeff;padding-top:20px;}

.button {width:100px;margin-top:0px;}

body {
margin:0;
margin-top:100px;
}
</style>

<script language="javascript">

function validate(evt)
{
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
var backslsh = key;
var chck = String.fromCharCode( key );

var regex = /[0-9]|\./;
if( !regex.test(chck) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}


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


function fncSum2()
{
var amount1=form2.text1.value;
var amount2=form2.text2.value;
var a = amount1.replace(/[^0-9\.]/g,"");
var b = amount2.replace(/[^0-9\.]/g,"");
var nStr = (Number(a) + Number(b)).toString();
var rgx = /(\d+)(\d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, '$1,$2');
}
//nStr = "$" + nStr; // if $ prefix required
if (nStr.indexOf('.') == -1) {  // if whole number add .00
nStr = nStr + ".00"
}
nStr = nStr.replace(/(\.\d)$/,"$10");  // if only one DP add another 0

return nStr
}

</script>

</head>

<body onload="form2.text1.focus()" >
<center>
<p>Enter 2100.49 in Amount</p>
<p>Enter 100.20 in Tax</p>
<p> and press + button<br>
It shows 2,200.6,899,999,999,996<br>
but I need 2,200.69</p>

</p>
<div id="box1">
<form name=form2>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td>Amount</td>

<td><input type=text name=text1 size=15 value="" onkeypress='validate(event)' onfocus="this.select

()" onblur="form2.text1.value=addCommas(form2.text1.value)" > </td></tr> 

<tr><td>Tax</td> <td><input type=text name=text2 size=15 value="" onkeypress='validate(event)' 

onblur="form2.text2.value=addCommas(form2.text2.value)"></td></tr> 
<tr><td>Result</td><td><input type=text name=text3 size=15 value="" disabled></td></tr>

</table>
<hr>
<input class="button" type=button name=command1 value="+" onclick="form2.text3.value=fncSum2()"><br>

<input class="button" type=button name=command8 value="Focus" onclick="form2.text1.select

()";"form2.text1.focus()"><br>
<input class="button" type=reset name=command9 value="Reset">

</form>
</div>

</center>
</body>
</html>
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by stbuchok
0

All numbers in JavaScript are floating point numbers, making arithmetic operations inaccurate at times.

Change line 60 to the following:

var nStr = (parseFloat(Number(a) + Number(b)).toFixed(2)).toString();

This is assuming you want a max of 2 decimal places.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.