• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your purpose to round fraction closest .50 then why not just cast your double value to int and add .50 end of it?
I am not sure this will help you or not.

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

Anyway, here is code

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using doubles for currency is a really bad idea. You're going to end up with glaring inaccuracies.
 
Karen Wilson
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should I use instead of double?
 
ibrahim yener
Ranch Hand
Posts: 202
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Wilson wrote:what should I use instead of double?

how about using float
 
Tim Cooke
Sheriff
Pie
Posts: 3203
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic