• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Rounding off $$ amounts

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've managed to create a self calculating form but I'm stuck on getting the calculation amounts to round off to 2 decimal places. In the example I've pasted below, I need to make the sales tax and grand total amounts round off to 2 decimal places since it is displaying money. The other total fields I don't need to worry about since, unless the price changes, they will never be anything but a whole value. FYI, the sales tax is based on what the user selects in the state field farther up in the form.
Can someone help me?

[ September 24, 2003: Message edited by: Scott Christy ]
[ September 25, 2003: Message edited by: Eric Pascarello ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yourAns = Math.round(yourVar * 100)/100;
this will round your answer but will not display extra zeros if needed.
So you need to use this too...

See if that helps you
[ September 25, 2003: Message edited by: Eric Pascarello ]
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the code. I think I see what you're doing but I'm not sure about how to put it in my code. I'll give it a try this afternoon and let you know how it goes.
Thanks again for trying to help me.
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That worked perfect for rounding the values to the nearest dollars and cents. I'm left with one last problem. When the subtotal and the tax are added together it appears to be rounding off to the nearest whole dollar value. For example, in the script below, if the subtotal calculates to 30 and the tax is 1.65 the answer given for the total is 31. It shoudl be 31.65.

[edited to fit page: Eric]
[ September 29, 2003: Message edited by: Eric Pascarello ]
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the line that says:
form.grandtotal.value = Math.round((parseInt(form.tax.value) + parseInt(form.subtotal.value)) * 100)/100;
to
form.grandtotal.value = Math.round((parseFloat(form.tax.value) + parseFloat(form.subtotal.value)) * 100)/100;
and it seems to work ok. Is this the right technique to use?
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, the int does not carry decimal places....float does...
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to know. Eric, Thanks for all your help.
 
permaculture is largely about replacing oil with people. And one tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic