Restrict to 2 decimal places ! How

# Thread: Restrict to 2 decimal places ! How

1. Daza Guest

## Restrict to 2 decimal places ! How

Hello all<BR><BR>I am filling a text box with some values and would like to restrict the result of my equasion to 2 decimal places .<BR>In vb i can FormatNumber(myVal,2) to do this how can i do this with J ?<BR><BR>Heres the code<BR>&#060;script&#062;<BR>function addUp(value,name){<BR>var val = new Number(value)<BR>if(eval("document.forms[0]."+name+".checked == true")){<BR>if(document.all.outpay.value == "" ){<BR>document.all.outpay.value = val<BR>}<BR>else{<BR>var num = new Number(document.all.outpay.value)<BR>num += val<BR>document.all.outpay.value = num<BR>}<BR>}<BR>else<BR>{<BR>var num = new Number(document.all.outpay.value)<BR>num = num - val<BR>if(num &#060;2){<BR>num = 0<BR>}<BR>document.all.outpay.value = num<BR>}<BR>}<BR>&#060;/script&#062;<BR><BR>Hope you can help

2. Dan Evans Guest

## Here's a one line function for it

The code below restricts the number to x decimal places<BR><BR>&#060;script&#062;<BR>var val = 12.34567<BR>alert(xdp(val, 3)); // Shows 12.346<BR><BR>function xdp(num, x) {<BR> return Math.round(num * Math.pow(10, x)) / Math.pow(10, x); <BR>}<BR>&#060;/script&#062;<BR><BR>Hope this helps,<BR><BR>-Dan Evans

3. Daza Guest

## AHA !!!!!!!!

Thanks m8ty it orks just fine.<BR><BR>:-{()

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•