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.

mskfisher
  • 3,291
  • 4
  • 35
  • 48
clerksx
  • 632
  • 4
  • 13
  • 21
  • 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? – Daniel Fischer Dec 01 '11 at 20:02

8 Answers8

18

Why not try regexp?

new Float(10.25000f).toString().replaceAll("\\.?0*$", "")
Nthalk
  • 3,774
  • 1
  • 25
  • 19
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"
maerics
  • 151,642
  • 46
  • 269
  • 291
  • 1
    +1 for DecimalFormat. Additionally decimal format using half even rounding. http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html – HRgiger Dec 01 '11 at 20:27
  • Thanks a lot. I did not clarify that I was actually working on Processing. The regexp was simpler flor my case. – clerksx Dec 01 '11 at 21:17
2

You just need to use format class like following:

new java.text.DecimalFormat("#.#").format(10.50000);
Paras Santoki
  • 821
  • 11
  • 26
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);
Sarel Botha
  • 12,419
  • 7
  • 54
  • 59
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();
Shaun
  • 2,446
  • 19
  • 33
1

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

Udo Held
  • 12,314
  • 11
  • 67
  • 93
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
Community
  • 1
  • 1
0

Try using System.out.format

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

Sid Malani
  • 2,078
  • 1
  • 13
  • 13