How to format Double with dot?

前端 未结 3 619
挽巷
挽巷 2020-12-24 01:39

How do I format a Double with String.format to String with a dot between the integer and decimal part?

String s = String.format(\"%.2f\", price)         


        
3条回答
  •  孤城傲影
    2020-12-24 02:20

    Based on this post you can do it like this and it works for me on Android 7.0

    import java.text.DecimalFormat
    import java.text.DecimalFormatSymbols
    
    DecimalFormat df = new DecimalFormat("#,##0.00");
    df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ITALY));
    System.out.println(df.format(yourNumber)); //will output 123.456,78
    

    This way you have dot and comma based on your Locale

    Answer edited and fixed thanks to Kevin van Mierlo comment

提交回复
热议问题