Java String format float type

Description

Java String format float type

public class Main {
    public static void main(String[] args) {
        float number = 12345.98765f;
        String numberAsString = String.format ("%.2f", number);
        System.out.println(numberAsString);
        //  w  w  w  .j a va 2s . co m
        numberAsString = String.format ("%,.2f", number);
        System.out.println(numberAsString);
        
        System.out.println(String.format ("%,.2f", 12345678.12345f));
        System.out.println(String.format ("%,.2f", 9876543.98765f));
    }
}



PreviousNext

Related