Results 1 to 3 of 3
  1. #1
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default How do I round a number to nearest 2.5 multiple?

    I'v having a program that supports calculating weight in kg for a training program(powerlifting).
    When it prints the numbers I get "119.69999999999999" and "136.8" etc. I want those to be "120" and "137,5", since you can't load the barbell with such kg-amounts in any gym I've been to.
    So I need to put in a code that rounds up/down to nearest 2,5kg multiple.
    Been trying to search for it, but can't seem to find answers for anything but rounding to an integer value.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How do I round a number to nearest 2.5 multiple?

    One way is to multiply your number by a number that will allow rounding by Math.round(...) to do the correct rounding, and then divide by that number before using String.format(...) or DecimalFormat to produce a nice String representation. You could multiply the number by 0.4 or divide it by 2.5, then round it to the nearest int, then divide the result by 0.4 or multiply it by 2.5 to get the result, but then use a String formatting tool to round this to 1 decimal place. String.format("%.1f", myNumber) could work well for this.
    Last edited by Fubarable; 11-25-2012 at 12:51 AM.

  3. #3
    LasseA is offline Member
    Join Date
    Oct 2012
    Posts
    29
    Rep Power
    0

    Default Re: How do I round a number to nearest 2.5 multiple?

    Cool, thanks for answer, will try those suggestions :)

Similar Threads

  1. Replies: 6
    Last Post: 09-28-2012, 02:21 PM
  2. Splitting an string of number into multiple lines
    By Juukamen in forum New To Java
    Replies: 5
    Last Post: 04-09-2012, 11:30 AM
  3. How to round up floating number properly?
    By IndraG in forum New To Java
    Replies: 4
    Last Post: 01-19-2012, 08:57 AM
  4. going round and round in class..
    By sonny in forum New To Java
    Replies: 52
    Last Post: 04-02-2010, 11:56 AM
  5. Need nearest neighbor code
    By reddish in forum New To Java
    Replies: 4
    Last Post: 09-20-2008, 03:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •