comma or dot when transform float to String.

edited September 2016 in Questions about Code

When I transforme a float to String with two differents methods. The first return a String with dot and the second return a String with comma, it's weird. It's only on OSX or the result is the same or all platfrom ?

float f = 1.2 ;
String s_1 = Float.toString(f);
String s_2 = String.format("%.2f", f) ;
println(s_1) ;
println(s_2) ; 

Answers

  • Answer ✓

    it's a Locale thing.

    https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html

    import java.util.Locale;
    import java.text.NumberFormat;
    
    float f = 1.2 ;
    
    Locale french = new Locale("fr");
    NumberFormat frenchNF = NumberFormat.getInstance(french);
    println("french: ");
    println(frenchNF.format(f)) ;
    
    Locale english = new Locale("en");
    NumberFormat englishNF = NumberFormat.getInstance(english);
    println("english: ");
    println(englishNF.format(f)) ;
    
  • Thx for the answer, sorry for my lating thx :)

Sign In or Register to comment.