java – How to print a decimal number with dot, not comma?

Question:

If I do:

float a=5;
System.out.printf("%d", a);

Its output will be:

5.000000

How do I print 5.000000 ? That is, I want to replace the comma with a period.

Answer:

Maybe this will help you:

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

public class Teste {
    public static NumberFormat seuFormato() {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ROOT);
        symbols.setDecimalSeparator(',');
        symbols.setGroupingSeparator('.');
        return new DecimalFormat("#0.00", symbols);
    }

    public static void main(String[] args) {
        NumberFormat formatter = seuFormato();
        float a = 5;
        System.out.println(formatter.format(a));
    }
}

See it working on Ideone here.

Taken from an example from the Oracle website .

Scroll to Top
AllEscort