Click to See Complete Forum and Search --> : Help with code...Can't round it up to 2 decimal places.


Newbie78
05-02-2006, 02:52 AM
Can someone please help me with this code of mine??? :confused:
Can't seem to round the sum to two decimal places of this code below:

document.Formular.Total.value =(wert1*1)+(wert2*1)

It actually works with the first bit of the code:

var k = (Math.round(temp * 20) / 20).toString();
k += (k.indexOf('.') == -1)? '.00' : '00';
return k.substring(0, k.indexOf('.') + 3);
return k;

Please help me....Thanks! :rolleyes:

See full code below:


<SCRIPT language=JavaScript>
<!--
function printit(){
window.print()
}
// -->
</SCRIPT>

<SCRIPT language=JavaScript>
<!--
function Wert(zahl1,zahl2) {
var temp=zahl1*zahl2*120/10000;
var k = (Math.round(temp * 20) / 20).toString();
k += (k.indexOf('.') == -1)? '.00' : '00';
return k.substring(0, k.indexOf('.') + 3);
return k;
}

function Berechne() {
var wert1 = Wert(document.Formular.Hoehe1.value,document.Formular.Breite1.value);
document.Formular.Ergebnisfeld1.value = wert1;
var wert2 = Wert(document.Formular.Hoehe2.value,document.Formular.Breite2.value);
document.Formular.Ergebnisfeld2.value = wert2;
document.Formular.Total.value =(wert1*1)+(wert2*1)
}
//-->
</SCRIPT>

sridhar_423
05-02-2006, 02:58 AM
var k = (parseFloat(Math.round(temp * 20) / 20).toFixed(2)).toString();


multiplying with 20 and dividing by 20 ..??

Newbie78
05-02-2006, 03:28 AM
Hi. Thanks for the quick reply. Honestly I don't know how i got into that :p

You see if I compute:

function Wert(zahl1,zahl2) {
var temp=zahl1*zahl2*120/10000;
var k = (Math.round(temp * 20) / 20).toString();
k += (k.indexOf('.') == -1)? '.00' : '00';
return k.substring(0, k.indexOf('.') + 3);
return k;

------------------------

e.g. (150,233)
temp=150*233*120/100000;
k=419.4 --- > until this code --> var temp=zahl1*zahl2*120/10000;

and after i added this bit:

var k = (Math.round(temp * 20) / 20).toString();
k += (k.indexOf('.') == -1)? '.00' : '00';
return k.substring(0, k.indexOf('.') + 3);
return k;

gave me the result : k=419.04

I need your patience on this one..I am pretty new with this.

I need to get the Total.value also rounded off to 2 decimal places. Ican't seem to make it work :(

Total.value =(wert1*1)+(wert2*1)

What else do i need to do??? Thanks in advance..I really appreciate your help.

sridhar_423
05-02-2006, 03:56 AM
Total.value =parseFloat((wert1*1)+(wert2*1)).toFixed(2);

sridhar_423
05-02-2006, 03:57 AM
parseFloat(Some_Value).toFixed(n) will round off "Some_value" to "n" decimals.

Newbie78
05-04-2006, 10:31 AM
thank you so much. it worked!