Results 1 to 12 of 12
  1. #1
    NuxlyStardust is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Displaying floating point value

    Hello there :)

    I have float variables which I need to display (and thus convert to string), and I'm looking for what would be the right way to format the output nicely.
    I want it to have only one digit after the separator, and not have anything if there is no decimal value.

    So 25.37000 would output 25.3, and 37.0 would output 37.

    I can limit the output to one digit after the dot like this:
    String.format("%.1f", myVariable);
    But the problem is that will still have the ".0" at the end, even if the value is round.

    I could go with something like replace(",0", "") but somehow I feel there's a better way to do it. Calling the replace function just to remove two characters from what used to be a float variable seems a bit overkill and unadapted.

    Any idea?

    Thanks :)

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    have you tried "%.0f" for 0 decimal places?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    have you tried "%.0f" for 0 decimal places?
    Nope, as Kevin said, a DecimalFormat String e.g. "0.#" displays at most one decimal fractional digit. Come to think of it: the format( ... ) method can't do that; I'll check it out if that is so.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    But i'm using "%.0f" and i get single digit, no decimal, from a double value.

    Just confirmed this by replacing the %.0f with %f and now it comes up as 1.0000000000 for item quantity rather than 1

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    But i'm using "%.0f" and i get single digit, no decimal, from a double value.

    Just confirmed this by replacing the %.0f with %f and now it comes up as 1.0000000000 for item quantity rather than 1
    True, but the OP wants "3." if the number equals 3.0 and "3.1" if the number equals a number less than 3.15

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Yeah, I was thinking that step one was using DecimalFormat, and step two was checking the last digit and then using substring. There might be a better way, but that seems the most obvious to me.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    How about:

    Java Code:
    if((myVariable-(int)myVariable) == 0)
        string = ""+((int)myVariable);
    else
        string = String.format("%.1f", myVariable);

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    :headdesk:

    Why are you suggesting to use overly complicated code when the correct solution has already been suggested? Use DecimalFormat.

    Sheesh. Please refrain from replying when you do not know what you are talking about.

  10. #10
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Wait a minute......I thought someone said DecimalFormat doesn't work for this....or was it String.format()....? Woops :D

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Quote Originally Posted by ra4king View Post
    Wait a minute......I thought someone said DecimalFormat doesn't work for this....or was it String.format()....? Woops :D
    Did you not read the other posts before adding your own reply? The post right before yours outlines a pretty clear way to do it.

    Spoonfeeding does NOT help the OP. Spoonfeeding incorrect answers is really harmful.

    This is the second time I've seen you put forth incorrect information today. It's one thing if they're honest mistakes, but if I were you, I might hesitate to offer help until I was sure I knew what I was talking about.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Incorrect answers? Where? My code reply here still works and I don't remember any other incorrect answer I have given.
    And I just misunderstood a post, therefore leading to my answer.

Similar Threads

  1. Floating point inaccuracy
    By Aga in forum Advanced Java
    Replies: 6
    Last Post: 02-04-2011, 05:07 AM
  2. if it is floating point number
    By Stephen Douglas in forum New To Java
    Replies: 26
    Last Post: 07-06-2010, 09:17 AM
  3. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 04:11 PM
  4. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 09:41 AM
  5. Floating point values in SWT Spinner
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:50 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
  •