Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need the sum to include 100th digit in a sum

Status
Not open for further replies.

rampage60

IS-IT--Management
Jul 1, 2002
73
NO
If the equitions = 150.10 it will leave off the 0. Like this 150.1 . Our accounting dept. won't except this. How do I make keep the 100th digit intact? Thnx!

JS Newbiew.

<html>
<head>

<SCRIPT LANGUAGE=&quot;javascript&quot;>
function add()
{
if(document.expense.misc01.value==&quot;&quot;)
document.expense.misc01.value= 0;
if(document.expense.misc02.value==&quot;&quot;)
document.expense.misc02.value= 0;
if(document.expense.misc03.value==&quot;&quot;)
document.expense.misc03.value= 0;

var mpt1 = document.expense.misc01.value;
var mpt2 = document.expense.misc02.value;
var mpt3 = document.expense.misc03.value;
var sumaa = Math.round(mpt1*100)/100 + Math.round(mpt2*100)/100 + Math.round(mpt3*100)/100;

document.expense.total.value =Math.round(sumaa*100)/100

}
</script>

</head>

<body>

<div align=&quot;center&quot;>
<form name=&quot;expense&quot;>
Field 1<input name=&quot;misc01&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Field 2<input name=&quot;misc02&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Field 3<input name=&quot;misc03&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Sum of All Fields <input name=&quot;total&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
<input type=&quot;button&quot; value=&quot;Do It&quot; onClick=&quot;add()&quot;>
</div>
</body>
</html>
 

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
I don't see how to incorporate that example in this current script. :( That exampl just seems to add commas. What will it take to make this script to leave the 0's on the answer?
 
hmm.. wonder why I had that bookmarked under dec formatting. apologies.

try this. it's fairly self explaining. bottom line is what you are doing there is nothing more then rounding to 2 decimals but not formatting to 2 decimals. you need to check for the place holding and set the value to what you find in the conditioning.

example:
<html>
<head>

<SCRIPT LANGUAGE=&quot;javascript&quot;>

function CurrencyFormatted(amount) {
var i = parseFloat(amount);

if(isNaN(i)) {
i = 0.00;
}

var minus = '';
if(i < 0) {
minus = '-';
}

i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);

if(s.indexOf('.') < 0) {
s += '.00';
}

if(s.indexOf('.') == (s.length - 2)) {
s += '0';
}

s = minus + s;
return s;
}


function add() {
var mpt1 = document.expense.misc01.value;
var mpt2 = document.expense.misc02.value;
var mpt3 = document.expense.misc03.value;

if(mpt1==&quot;&quot;) {
mpt1 = 0;
alert(mpt1)
}

if(mpt2==&quot;&quot;) {
mpt2 = 0;
alert(mpt2)
}

if(mpt3==&quot;&quot;) {
mpt3 = 0;
alert(mpt3)
}
var sumaa = CurrencyFormatted(parseFloat(mpt1) + parseFloat(mpt2) + parseFloat(mpt3));

document.expense.total.value = sumaa;

}
</script>

</head>

<body>

<div align=&quot;center&quot;>
<form name=&quot;expense&quot;>
Field 1<input name=&quot;misc01&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Field 2<input name=&quot;misc02&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Field 3<input name=&quot;misc03&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
Sum of All Fields <input name=&quot;total&quot; type=&quot;text&quot; size=&quot;10&quot;><br>
<input type=&quot;button&quot; value=&quot;Do It&quot; onClick=&quot;add()&quot;>
</div>
</body>
</html>

reference for modification

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
That does work but I find using toFixed() easier.
 
Hey, glad to hear it.

be careful as I do not believe the toFixed() function is very browser supported yet.

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
Refer to thread216-518499 to get a more cross-browser friendly version of toFixed().

Adam
while(woman.width>woman.height && wallet.value>0){beer++;vision.blur()};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top