• 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

How to round a float with a 0.25 step

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to round a float in this way:

2.0----> 2.0
2.125-> 2.0
2.25--> 2.25
2.375-> 2.25
2.5----> 2.5
2.625-> 2.5
2.75--> 2.75
2.875-> 2.75
3.0----> 3.0

Can someone help me please ?
I am writing a lot of code, but I am sure there is a "smart" way to do that!! ;)

Thanks!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using floats rather than doubles? Try multiplying by 4, adding 0.125, using the floor(double) method, and dividing by 4 again. You may, however, introduce imprecision into your results like that. Note you will end up with a double by that technique.
 
Alessandro Camel
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I end up with something like this:


output:


I am using float, because these are really simple numbers: the larger number used in the program could be 15! ;)
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiply by 4, floor (not round), divide by 4 again. You'll get (original, *4, rounded, /4):
As for the imprecision that Campbell was talking about, I don't think that will happen since you're only working with multiples of 2. These can be represented 100% accurately in doubles (as long as neither the integer part becomes too large or the decimal part becomes too small).
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should still not use floats, even for small numbers. Where are you getting 0.13 from? That will not give the right answer at all.
You will get imprecision for numbers like 2.3, but presumably, as Rob suggests, the “floor”ing will get rid of that.
I think I should have said add 0.5, not 0.125. Sorry for that mistake.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never said that flooring would get rid of precision problems (although it probably will). What I said is that because the decimal parts are all of a form of a*1/2 + b*1/4 + c*1/8, these can be represented in float / double with accurate precision, as long as the integer part doesn't cause it to be cut off.

As for float, because these numbers are so small, there will also be no problem with precision loss.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we actually agree, Rob. Only I am not expressing myself clearly.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is another approach, using a NavigableSet:

 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic