6

I'd like to change float like this way:

10.5000 -> 10.5 10.0000 -> 10

How can I delete all zeros after the decimal point, and change it either float (if there's non-zeros) or int (if there were only zeros)?

Thanks in advance.

1
  • 6
    I don't quite understand. The zeros are just a product of the textual representation, they have nothing to do with how the float is internally represented. Are you looking for custom output formats suppressing trailing zeros? Dec 1, 2011 at 20:02

8 Answers 8

18

Why not try regexp?

new Float(10.25000f).toString().replaceAll("\\.?0*$", "")
5
  • This doesn't quite make sense because new Float(10.25000f).toString() already gives you "10.25" so .replaceAll("0*$", "") is of no use. Dec 1, 2011 at 20:17
  • 1
    "\.0+$" if you want to also get rid of the period.
    – Marcelo
    Dec 1, 2011 at 20:17
  • If I change "0*$" to "\.0+$", it says "unexpected char: '.'" - I'm on Processing actually.
    – clerksx
    Dec 1, 2011 at 21:01
  • 1
    @user1076206 Must escape the backslash, "\\.0+$". Dec 1, 2011 at 21:11
  • Thanks Daniel, it worked. I also used replaceAll("0*$", "").replace(".", " "). Actually for visual reason, I added a white space at the end.
    – clerksx
    Dec 1, 2011 at 21:16
18

Well the trick is that floats and doubles themselves don't really have trailing zeros per se; it's just the way they are printed (or initialized as literals) that might show them. Consider these examples:

Float.toString(10.5000); // => "10.5"
Float.toString(10.0000); // => "10.0"

You can use a DecimalFormat to fix the example of "10.0":

new java.text.DecimalFormat("#").format(10.0); // => "10"
2
2

java.math.BigDecimal has a stripTrailingZeros() method, which will achieve what you're looking for.

BigDecimal myDecimal = new BigDecimal(myValue);
myDecimal.stripTrailingZeros();
myValue = myDecimal.floatValue();
2

This handles it with two different formatters:

double d = 10.5F;
DecimalFormat formatter = new DecimalFormat("0");
DecimalFormat decimalFormatter = new DecimalFormat("0.0");
String s;
if (d % 1L > 0L) s = decimalFormatter.format(d);
else s = formatter.format(d);

System.out.println("s: " + s);
2

You just need to use format class like following:

new java.text.DecimalFormat("#.#").format(10.50000);
1

Format your numbers for your output as required. You cannot delete the internal "0" values.

0

Try using System.out.format

Heres a link which allows c style formatting http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

0

I had the same issue and find a workaround in the following link: StackOverFlow - How to nicely format floating numbers to string without unnecessary decimal 0

The answer from JasonD was the one I followed. It's not locale-dependent which was good for my issue and didn't have any problem with long values.

Hope this help.

ADDING CONTENT FROM LINK ABOVE:

public static String fmt(double d) {
    if(d == (long) d)
        return String.format("%d",(long)d);
    else
        return String.format("%s",d);
    }

Produces:

232
0.18
1237875192
4.58
0
1.2345
1
  • Can you add some content from the link?
    – Robert
    Jul 23, 2015 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.