Display a value with the non-zeroed cents and using Float?

1

Is there any way to keep cents from being cut with Float?

I'm converting a value to be formatted with DecimalFormat so it looks like the cents like this:

    Float numero = 2564786549.87543771885808f;
    DecimalFormat df = new DecimalFormat("0.00");  
    System.out.println(df.format(numero));

I need this to appear:

2564786432,54

But this way it's zeroing the cents, how could you keep zero cents?

Ps: I can not use double.

    
asked by anonymous 09.05.2017 / 15:12

1 answer

1

Hello, I recommend reading Formatting Numeric Print Output will be useful!

Excerpt taken from the source: These methods, format and printf equivalent to one another!

import java.util.Calendar;
import java.util.Locale;

public class TestFormat {

    public static void main(String[] args) {
      long n = 461012;
      System.out.format("%d%n", n);      //  -->  "461012"
      System.out.format("%08d%n", n);    //  -->  "00461012"
      System.out.format("%+8d%n", n);    //  -->  " +461012"
      System.out.format("%,8d%n", n);    // -->  " 461,012"
      System.out.format("%+,8d%n%n", n); //  -->  "+461,012"

      double pi = Math.PI;

      System.out.format("%f%n", pi);       // -->  "3.141593"
      System.out.format("%.3f%n", pi);     // -->  "3.142"
      System.out.format("%10.3f%n", pi);   // -->  "     3.142"
      System.out.format("%-10.3f%n", pi);  // -->  "3.142"
      System.out.format(Locale.FRANCE,
                        "%-10.4f%n%n", pi); // -->  "3,1416"

      Calendar c = Calendar.getInstance();
      System.out.format("%tB %te, %tY%n", c, c, c); // -->  "May 29, 2006"

      System.out.format("%tl:%tM %tp%n", c, c, c);  // -->  "2:34 am"

      System.out.format("%tD%n", c);    // -->  "05/29/06"
    }
}
    
04.07.2017 / 21:49