# How to make the numbers in textbox as 2 decimal points ? [Answered]RSS

## 5 replies

Last post May 18, 2012 08:03 AM by Ruchira

Member

76 Points

1264 Posts

### How to make the numbers in textbox as 2 decimal points ?

May 16, 2012 04:54 PM|jeffcarter|LINK

I am using this java script in my webpage to add 2 numbers. everything is working fine, and even if i put the decimal number, the result is in decimal.

<script type="text/javascript">
\$(function () {
var textBox1 = \$('input:text[id\$=TextBox1]').keyup(foo);
var textBox2 = \$('input:text[id\$=TextBox2]').keyup(foo);

function foo() {
var value1 = textBox1.val();
var value2 = textBox2.val();
var sum = add(value1, value2);
\$('input:text[id\$=TextBox15]').val(sum);
}

function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum += parseFloat(arguments[i]);
}
}
return sum;
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
});
</script>

But how to make the entered numbers and results in 2 decimal points by DEFAULT ?

Right now if I enter

Text Box: 1

Text Box: 2

Result: 3

I want:

Text Box: 1.00

Text Box: 2.00

Result: 3.00

How ?

Contributor

4392 Points

933 Posts

### Re: How to make the numbers in textbox as 2 decimal points ?

May 16, 2012 05:31 PM|vipindas|LINK

Change return sum to return sum.toFixed(2);

```<script type="text/javascript">
\$(function () {
var textBox1 = \$('input:text[id\$=TextBox1]').keyup(foo);
var textBox2 = \$('input:text[id\$=TextBox2]').keyup(foo);

function foo() {
var value1 = textBox1.val();
var value2 = textBox2.val();
var sum = add(value1, value2);
\$('input:text[id\$=TextBox15]').val(sum);
}

function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum += parseFloat(arguments[i]);
}
}
return sum.toFixed(2);
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
});
</script>```

Contributor

3372 Points

1155 Posts

### Re: How to make the numbers in textbox as 2 decimal points ?

May 16, 2012 05:33 PM|vijay_myl|LINK

hi..

Use this code..

```function chk()
{
var txt=document.getElementById("TextBox1");
txt.value=  parseFloat(txt.value).toFixed(2);

alert (i.value);
}```

if you pass the value 2 it returns 2.00

` parseFloat(txt.value).toFixed(2); it makes 2 decimal`

If the post is useful don't forget to MARK as Answer
My .NET blog

Member

76 Points

1264 Posts

### Re: How to make the numbers in textbox as 2 decimal points ?

May 16, 2012 07:39 PM|jeffcarter|LINK

In my code above, the only problem is if I am adding 2 numbers, the result is not calculated untill the last number (2nd number is entered)

In other words, if I wanna add 4 values in 4 textboxes, the result in Textbox5 is not shown untill n unless the 4th value in the textbox is entered. why ? any solution ?

Member

76 Points

1264 Posts

### Re: How to make the numbers in textbox as 2 decimal points ?

May 17, 2012 01:57 PM|jeffcarter|LINK

ok so following changed the result/output into 2 decimal points which i wanted:

```<script type="text/javascript">
\$(function () {
var textBox1 = \$('input:text[id\$=TextBox1]').keyup(foo);
var textBox2 = \$('input:text[id\$=TextBox2]').keyup(foo);

function foo() {
var value1 = textBox1.val();
var value2 = textBox2.val();
var sum = add(value1, value2);
\$('input:text[id\$=TextBox15]').val(sum);
}

function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
if (IsNumeric(arguments[i])) {
sum += parseFloat(arguments[i]);
}
}
return sum.toFixed(2);
}
function IsNumeric(input) {
return (input - 0) == input && input.length > 0;
}
});
</script>```

`Now how to change the input 2 values into 2 decimal points ?`

All-Star

49937 Points

9343 Posts

MVP

### Re: How to make the numbers in textbox as 2 decimal points ?

May 18, 2012 08:03 AM|Ruchira|LINK

#### jeffcarter

`Now how to change the input 2 values into 2 decimal points ?`

Hello,

onchange event of the textbox, call the below javascript function

```function mathRoundForTaxes(source) {
var txtBox = document.getElementById(source);
var txt = txtBox.value;
if (!isNaN(txt) && isFinite(txt) && txt.length != 0) {
var rounded = Math.round(txt * 100) / 100;
txtBox.value = rounded.toFixed(2);
}
}```
`<asp:TextBox ID="txtTaxRate" runat="server" OnKeyPress="return isNumberKey(this, event);" onchange="mathRoundForTaxes(this.id);"></asp:TextBox>`

and the below function OnKeyPress event

```function isNumberKey(sender, evt) {
var txt = sender.value;
var dotcontainer = txt.split('.');
var charCode = (evt.which) ? evt.which : event.keyCode;
if (!(dotcontainer.length == 1 && charCode == 46) && charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}```