Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 15 of 15

06232004, 04:15 PM #1
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
Rounding calculation to 2 decimals
I have this javascript for a form calculation:
<SCRIPT LANGUAGE="JavaScript">
<!
function roundoff(amount) {
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*16 == Math.floor(amount*17)) ? amount + '0' : amount);
}
function total(what,number) {
var grandTotal3 = 0;
var grandTotal2 = 0;
var grandTotal1 = 0;
var grandTotal = 0;
for (var i=0;i<number;i++) {
if (what.elements['price' + i].value == '')
what.elements['price' + i].value = '0.00';
what.elements['subtotal' + i].value=(what.elements['quantity' + i].value  0) * (what.elements['price' + i].value  0);
if (what.elements['quantity' + i].value == "0")
what.elements['subtotal' + i].value = "0.00";
subtotal=what.elements['subtotal' + i].value
grandTotal += (what.elements['price' + i].value  0) * (what.elements['quantity' + i].value  0)*1.1
grandTotal1 += (what.elements['price' + i].value  0) * (what.elements['quantity' + i].value  0)*.1
grandTotal2 += (what.elements['price' + i].value  0) * (what.elements['quantity' + i].value  0)*.1
grandTotal3 += (what.elements['price' + i].value  0) * (what.elements['quantity' + i].value  0)*1.1;
}
subtotal=roundoff(Math.round(subtotal*Math.pow(17,2))/Math.pow(17,2));
what.grandTotal.value = roundoff(Math.round(grandTotal*Math.pow(17,2))/Math.pow(17,2));
what.grandTotal1.value = roundoff(Math.round(grandTotal1*Math.pow(17,2))/Math.pow(17,2));
what.grandTotal2.value = roundoff(Math.round(grandTotal2*Math.pow(17,2))/Math.pow(17,2));
what.grandTotal3.value = roundoff(Math.round(grandTotal3*Math.pow(17,2))/Math.pow(17,2));
}
//>
</SCRIPT>
I want the numbers in the subtotals, shipping, and grand total boxes to round off to only 2 decimal points.
I've read of the Math.round(100*x)/100 functions but i'm unsure of how to apply it. I'm just learning javascript and know next to nothing. If you can help please be as basic as possible. If you wish to see what the page looks like now go to: http://www.hypainc.com/orderform1.htm Just be sure not to actually place and order Thanks in advance.
06232004, 04:51 PM
#2
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
You will find that the forums search is a shortcut to finding the solutions to often asked questions. As well as Liorean and several other members have taken the time to provide a FAQ which you will find as a sticky at the top of each forum.
Here are two results.
And an example:
Code:<script type="text/javascript"> function toDecimal(num){ n = Math.round(num*100).toString(); n = n.substring(0,n.length2)+'.'+ n.substring(n.length2,n.length); return n; } alert(toDecimal(123.4567)) </script>
06232004, 05:06 PM
#3
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
I understand how the search works but i have no clue about where or how to configure the JS so it will work in my script. I'm just trying to get the numbers to round so i can finish this site and give it to someone.
06232004, 05:21 PM
#4
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
Code:what.elements['subtotal' + i].value=toDecimal((what.elements['quantity' + i].value  0) * (what.elements['price' + i].value  0));
06232004, 05:32 PM
#5
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
i added toDecimal(x) but now the calculations stop working
06232004, 05:38 PM
#6
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
What the heh is x ????
I just provided you with the solution. Replace that current line with the one I posted. And I just went and checked it with your page codes and it works. Also, did you even bother adding the toDecimal function I previously posted?
06232004, 05:44 PM
#7
 Join Date
 Oct 2003
 Location
 SC
 Posts
 936
 Thanks
 0
 Thanked 0 Times in 0 Posts
why dont you just multiply by 100 then round then divide by a 100...
hope this helps
06232004, 06:00 PM
#8
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
Willy i apologize for my ignorance. i thought i had to REPLACE the existing function with the one u posted before. It works and i am humbled by your genius. You are a god of gods.
06232004, 06:03 PM
#9
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
oh one more teeny tiny little thing. how do i round off the shipping and grand total at the bottom?
06232004, 06:20 PM
#10
 Join Date
 Jun 2004
 Posts
 7
 Thanks
 0
 Thanked 0 Times in 0 Posts
Nevermind i figured it out. Yay me lol. Seriously though Thank you So much Willy. You are my saviour.
06232004, 11:25 PM
#11
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
Hi;
I'm glad to hear you figured it out.
.....Willy
Last edited by Willy Duitt; 06232004 at 11:54 PM.
06242004, 02:48 AM
#12
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
Here try replacing your function with this one which includes validation that only a whole number and nothing else is entered into the quantity field as well as resets the fields if quantity is left blank or cut from the field using the contextmenu.
BTW: This goes into the head not the body where it currently resides.
.....WillyCode:<script type="text/javascript"> <!// function toDecimal(number){ // ROUND NUMBER TO TWO DECIMAL PLACES //; var number = Math.round(number*100).toString(); number = number.substring(0,number.length2)+'.'+ number.substring(number.length2,number.length); if(number == 0){ number = '0.00' }; return number; } function total(form){ var shipping = grandTotal = 0; // PROVIDES shipping & grandTotal AN INTIAL VALUE OF ZERO //; for(var i=0; i<=16; i++){ // LOOPS THRU ELEMENTS (CHECK THE NUMBERS YOU ARE PASSING) //; var price = form.elements['price' + i]; // REUSABLE VARIABLE //; var quantity = form.elements['quantity' + i]; // REUSABLE VARIABLE //; var subtotal = form.elements['subtotal' + i]; // REUSABLE VARIABLE //; if(quantity.value.length > 0 && !quantity.value.match(/^[09]+$/g)){ // CHECKS IF VALUE IS NOT EMPTY & IF IT IS A NUMBER OR NOT //; alert('Please enter only whole numbers in this field!'); subtotal.value = '0.00'; // RESETS FIELD IF NOT A NUMBER //; quantity.value = ''; // RESETS FIELD IF NOT A NUMBER //; } else{ // COMPLETES CALCULATIONS IF QUANTITY IS A NUMBER //; subtotal.value = toDecimal(quantity.value * price.value); grandTotal += ((quantity.value * price.value)*1.1); shipping += ((quantity.value * price.value)*.1); } quantity.onblur = function(){ // REVALIDATES QUANTITY WHEN USER LEAVES FIELD //; this.value = this.value.replace(/^[0]*/g,''); // REMOVES LEADING ZERO's IF FIELD IS NOT ZERO //; if(this.value.length == 0){ // CHECKS IF QUANTITY IS BLANK //; this.value = 0; // RESETS FIELD IF NOT A NUMBER //; } total(form); // FIRES FUNCTION AGAIN TO INSURE FIELDS ARE UPDATED //; } } form.grandTotal.value = toDecimal(grandTotal); // GRAND TOTAL form.grandTotal1.value = toDecimal(shipping); // SHIPPING form.grandTotal2.value = toDecimal(shipping); // SHIPPING 2 form.grandTotal3.value = toDecimal(grandTotal); // GRAND TOTAL 2 } //> </script>
Edit: Add validation to trim leading zero's
Last edited by Willy Duitt; 06242004 at 03:45 PM.
06242004, 03:53 AM
#13
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,037
 Thanks
 0
 Thanked 250 Times in 246 Posts
There's a better toDecimals() version in the Javascript FAQ. The method extends the Number object and is more flexible as you can pass the desired number of decimal places as function parameter.
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________
06242004, 03:06 PM
#14
 Join Date
 Sep 2003
 Posts
 3,620
 Thanks
 0
 Thanked 0 Times in 0 Posts
Hi Glenn;
Although I submit to your obvious expertise, I would have to disagree. Although this is only my personal opinion, I find my method easier to use and much more intuitive. And I do have another routine which allows the passing of the desired number of decimal places, although I rarely have need for more than two decimal places.
......WillyCode:<script type="text/javascript"> <!// function toDecimal(number,decimals){ var zeros = ''; for(var i=0; i<decimals; i++){ zeros += '0'; } var places = (1+zeros)*1; n = Math.round(number*places).toString(); n = n.substring(0,n.lengthdecimals)+'.'+ n.substring(n.lengthdecimals,n.length); return n; } alert(toDecimal(123.4567,3)) // EXAMPLE OF USE //; //> </script>
06252004, 03:28 AM
#15
 Join Date
 Jun 2002
 Location
 Philippines
 Posts
 11,037
 Thanks
 0
 Thanked 250 Times in 246 Posts
What if the passed parameters are not actually numbers (NaN)?
That could happen if the passed parameters are the results of some math operations of user inputs that could lead in nonnumeric output.
Using liorean's version is more flexible as well as elegant because the method only applies to Number object.
var n = 123.4321;
alert(n.toDecimals(3)); //alerts 123.432
var n2 = 123.4321;
alert(n2.toDecimals()); //alerts 123.43 (default is 2 decimal places)
var ans = prompt("Input number","");
var n3 = Number(ans);
alert(n3.toDecimals(1)); //alerts NaN if entered value is not numeric
Glenn
____________________________________
My Blog
Tower of Hanoi Android app (AdFREE!)
Tower of Hanoi Android app (FREE!)
Tower of Hanoi Leaderboard
Samegame Facebook App
Samegame Facebook App Leaderboard
vBulletin 5 Floating Bar Mod
vBulletin 5 Photo Slideshow Mod
vBulletin 5 Quote Selected Text Mod
vBulletin 5 Profile Preview Tooltip Mod
____________________________________