打印带有 2 位小数的 double 值。不是,
Printing a double value with 2 decimals with a . not a ,
我正在开发的应用程序有一点问题。我正在使用一些双精度值,我想将它们放在一些 TextViews
中,但是当放置数字时,而不是“。”它出现 ”,”。我想要一个点,这样我就可以用 substring
.
导入值
DecimalFormat precision = new DecimalFormat("0.00");
final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice);
double price = 200.12357123;
totalPrice.setText("$" + precision.format(price));
TextView
里面会出现200,12,我想出现200.12,这样以后可以把文字转换成double值。
试试这个
Locale currentLocale = Locale.getDefault();
String formatString = "#,###,###,###.##";
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
double price = 200.12357123;
Log.i("TAG", "" + "$ " + df.format(price));
输出
您可以更改“.”使用 ',' ,当你想转换为双精度时。
使用下面的代码,它会给出与您想要的完全相同的答案:
double price = 200.12357123;
String s=String.format(Locale.US, "$%.2f", price);
我正在开发的应用程序有一点问题。我正在使用一些双精度值,我想将它们放在一些 TextViews
中,但是当放置数字时,而不是“。”它出现 ”,”。我想要一个点,这样我就可以用 substring
.
DecimalFormat precision = new DecimalFormat("0.00");
final TextView totalPrice = (TextView) getView().findViewById(R.id.actualPrice);
double price = 200.12357123;
totalPrice.setText("$" + precision.format(price));
TextView
里面会出现200,12,我想出现200.12,这样以后可以把文字转换成double值。
试试这个
Locale currentLocale = Locale.getDefault();
String formatString = "#,###,###,###.##";
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale);
otherSymbols.setDecimalSeparator(',');
otherSymbols.setGroupingSeparator('.');
DecimalFormat df = new DecimalFormat(formatString, otherSymbols);
double price = 200.12357123;
Log.i("TAG", "" + "$ " + df.format(price));
输出
您可以更改“.”使用 ',' ,当你想转换为双精度时。
使用下面的代码,它会给出与您想要的完全相同的答案:
double price = 200.12357123;
String s=String.format(Locale.US, "$%.2f", price);