Decimal separator in NumberFormat

后端 未结 3 1777
萌比男神i
萌比男神i 2020-11-30 09:52

Given a locale java.text.NumberFormat:

NumberFormat numberFormat = NumberFormat.getInstance();

How can I get the character used as Decimal

3条回答
  •  眼角桃花
    2020-11-30 10:22

    The helper class DecimalFomatSymbols is what you are looking for:

    DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
    DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
    char sep=symbols.getDecimalSeparator();
    

    To set you symbols as needed:

    //create a new instance
    DecimalFormatSymbols custom=new DecimalFormatSymbols();
    custom.setDecimalSeparator(',');
    format.setDecimalFormatSymbols(custom);
    

    EDIT: this answer is only valid for DecimalFormat, and not for NumberFormat as required in the question. Anyway, as it may help the author, I'll let it here.

提交回复
热议问题