Java String.format with currency symbol

前端 未结 5 1874
广开言路
广开言路 2021-01-07 23:18

There is some existing code of the follow form which is used for format numerical values:

String.format( pattern, value )

5条回答
  •  忘掉有多难
    2021-01-07 23:47

    If there is no default Locale available, we can go with setting the currency symbol using unicode and decimal formatting. As in the below code:

    For e.g. Setting the Indian currency symbol and formatting the value. This will work without user making changes in the settings.

    Locale locale = new Locale("en","IN");
    DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
    DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
    dfs.setCurrencySymbol("\u20B9");
    decimalFormat.setDecimalFormatSymbols(dfs);
    System.out.println(decimalFormat.format(12324.13));
    

    Output:

    ₹12,324.13
    

提交回复
热议问题