DecimalFormat variable group size

前端 未结 2 1361
陌清茗
陌清茗 2020-12-06 23:23

I\'ve researched the subject somewhat before posting the question, but I couldn\'t find the answer.

Here is what I\'m trying to do:

input: a number 7-8 decim

相关标签:
2条回答
  • 2020-12-06 23:39

    I don't think you can use the DecimalFormat grouping separator for this. From the Javadoc:

    If you supply a pattern with multiple grouping characters, the interval between the last one and the end of the integer is the one that is used. So "#,##,###,####" == "######,####" == "##,####,####".

    0 讨论(0)
  • 2020-12-06 23:47

    You can use ICU4J library's com.ibm.icu.text.DecimalFormat class for this same purpose. It supports variable length group size. For your particular case:

    com.ibm.icu.text.DecimalFormatSymbols dfs = new com.ibm.icu.text.DecimalFormatSymbols();
    dfs.setGroupingSeparator(' ');
    com.ibm.icu.text.DecimalFormat df = new com.ibm.icu.text.DecimalFormat("0,000000,0", dfs); 
    

    This should print '0 123456 7' for a number like '1234567'.

    0 讨论(0)
提交回复
热议问题