How to print a dotted decimal number, not comma?

3

If I do:

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

The output of it will be:

  

5,000000

How do I print 5.000000 ? That is, I want to replace the comma by the dot.

    
asked by anonymous 23.06.2016 / 02:40

2 answers

4

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 here working on Ideone.

Retrieved from an example of the Oracle site .

    
23.06.2016 / 05:04
-1

I believe that using replace (), solves your problem an example below:

import java.io.*;

public class Test{
   public static void main(String args[]){
      String Str = new String("Welcome to Tutorialspoint.com");

      System.out.print("Return Value :" );
      System.out.println(Str.replace('o', 'T'));

      System.out.print("Return Value :" );
      System.out.println(Str.replace('l', 'D'));
   }

Result:

WelcTme tT TutTrialspTint.cTm
WeDcome to TutoriaDspoint.com
    
23.06.2016 / 02:44