How to format double value for a given locale and number of decimal places?

冷暖自知 提交于 2019-11-27 23:28:52

问题


How can I use NumberFormat to format a double value for a given Locale (default locale is sufficient) and for a given number of decimal places?

For example, I have these values:

double d1 = 123456.78;
double d2 = 567890;

And I want to print them in the following way, for example with US locale and 2 decimal places:

123,456.78
567,890.00

I don't actually care about rounding mode in this case, because these double values are gained from a BigDecimal with the given scale, so their number of decimal places is always lesser or equal than the number of decimal places I want to print.

Edit:

To make things a bit more clear, the thing is that I want to display money in a locale dependent way, but with a fixed number of decimal places. The example above showed, how the values should be formatted if the system locale is en_US. Now lets say, the system locale is cs_CZ (Czech), so the same numbers should be formatted in this way:

123 456,78
567 890,00

Now, how can I set up a NumberFormat to always display 2 decimal places, but to display thousands separator and decimal point based on the current locale?


回答1:


If you care to read NumberFormat's documentation, the solution would be obvious:

double d1 = 123456.78;
double d2 = 567890;
// self commenting issue, the code is easier to understand
Locale fmtLocale = Locale.getDefault(Category.FORMAT);
NumberFormat formatter = NumberFormat.getInstance(fmtLocale);
formatter.setMaximumFractionDigits(2);
formatter.setMinimumFractionDigits(2);
System.out.println(formatter.format(d1));
System.out.println(formatter.format(d2));
System.out.println(fmtLocale.toLanguageTag());

On my machine this prints out:

123 456,78
567 890,00
pl-PL

I believe this is what you are looking for, and you don't have to mess-up with patterns. I wouldn't do that - for instance there are locales which group digits by two, not by three (this is the reason we talk about grouping separator and not thousands separator).




回答2:


Adapted from Customizing Formats,

  public void localizedFormat(double value,Locale loc ) {

  NumberFormat nf = NumberFormat.getNumberInstance(loc);
  DecimalFormat df = (DecimalFormat)nf;
  df.applyPattern("###,###.00");
  String output = df.format(value);

  }

This function should give you the required formatting.




回答3:


You could use something like

DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance();
formatter.applyPattern("#,###,##0.00");
formatter.format(yourDoubleHere)



回答4:


converts a double value to a double value with any number of digits after decimal. I have created this method in a Utility class to access it through out the project.

public static double convertToDecimal(double doubleValue, int numOfDecimals)  {
        BigDecimal bd = new BigDecimal(doubleValue);
        bd = bd.setScale(numOfDecimals, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }


来源:https://stackoverflow.com/questions/10411414/how-to-format-double-value-for-a-given-locale-and-number-of-decimal-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!