# Thread: Displaying floating point value

1. Member
Join Date
Apr 2011
Posts
4
Rep Power
0

## 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. have you tried "%.0f" for 0 decimal places?

3. Originally Posted by ozzyman
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

4. 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

5. Originally Posted by ozzyman
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

6. 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.

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

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

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

10. Originally Posted by ra4king
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.

11. Incorrect answers? Where? My code reply here still works and I don't remember any other incorrect answer I have given.