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

# how to make converted price round UP to the nearest 50 cent

Ranch Hand
Posts: 70
• Number of slices to send:
Optional 'thank-you' note:
I created a simple currency converter app where the user enters the value in euro and the converted price is displayed in other currencies.
But how do I round up the numbers to the nearest 50 cent? so if 1 euro is equal to 1.3395 dollars how do I get the app to display 1.50 dollars?

Ranch Hand
Posts: 202
• Number of slices to send:
Optional 'thank-you' note:
If your purpose to round fraction closest .50 then why not just cast your double value to int and add .50 end of it?

int x = (int) aud; // if aud is 2.36622336 than you will get 2

Anyway, here is code

Sheriff
Posts: 67749
173
• Number of slices to send:
Optional 'thank-you' note:
Using doubles for currency is a really bad idea. You're going to end up with glaring inaccuracies.

Karen Wilson
Ranch Hand
Posts: 70
• Number of slices to send:
Optional 'thank-you' note:
what should I use instead of double?

ibrahim yener
Ranch Hand
Posts: 202
• Number of slices to send:
Optional 'thank-you' note:

Karen Wilson wrote:what should I use instead of double?

Sheriff
Posts: 5555
326
• Number of slices to send:
Optional 'thank-you' note:

ibrahim yener wrote:how about using float

Still suffers from the same rounding precision issues.

As a minimum, use BigDecimal. For a more elaborate (better) solution use a custom Type to represent money. I haven't used any of them, but there appears to be a number of libraries available to do this. On the success of joda-time I would be encouraged to explore joda-money as a viable solution.

 Consider Paul's rocket mass heater.