DD.*_*DD. 81 java currency number-formatting
有没有办法格式化小数如下:
100 -> "100"
100.1 -> "100.10"
Run Code Online (Sandbox Code Playgroud)
如果是圆数,则省略小数部分.否则格式化为两位小数.
duf*_*ymo 151
我建议使用java.text包:
double money = 100.1;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String moneyString = formatter.format(money);
System.out.println(moneyString);
Run Code Online (Sandbox Code Playgroud)
这具有特定于语言环境的额外好处.
但是,如果必须,如果它是一整块钱,则截断你得到的字符串:
if (moneyString.endsWith(".00")) {
int centsIndex = moneyString.lastIndexOf(".00");
if (centsIndex != -1) {
moneyString = moneyString.substring(1, centsIndex);
}
}
Run Code Online (Sandbox Code Playgroud)
Thi*_*ena 109
double amount =200.0;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
System.out.println(currencyFormatter.format(amount));
Run Code Online (Sandbox Code Playgroud)
要么
double amount =200.0;
System.out.println(NumberFormat.getCurrencyInstance(new Locale("en", "US"))
.format(amount));
Run Code Online (Sandbox Code Playgroud)
显示货币的最佳方式
产量
$ 200.00
如果您不想使用sign,请使用此方法
double amount = 200;
DecimalFormat twoPlaces = new DecimalFormat("0.00");
System.out.println(twoPlaces.format(amount));
Run Code Online (Sandbox Code Playgroud)
200.00
这也可以用(千分隔)
double amount = 2000000;
System.out.println(String.format("%,.2f", amount));
Run Code Online (Sandbox Code Playgroud)
2,000,000.00
ext*_*eon 46
我对此表示怀疑.问题是如果它是浮点数100则永远不是100,它通常是99.9999999999或100.0000001或类似的东西.
如果你想以这种方式格式化它,你必须定义一个epsilon,即与整数之间的最大距离,如果差异较小则使用整数格式,否则使用浮点数.
像这样的东西可以做到这一点:
public String formatDecimal(float number) {
float epsilon = 0.004f; // 4 tenths of a cent
if (Math.abs(Math.round(number) - number) < epsilon) {
return String.format("%10.0f", number); // sdb
} else {
return String.format("%10.2f", number); // dj_segfault
}
}
Run Code Online (Sandbox Code Playgroud)
小智 17
谷歌搜索后我没有找到任何好的解决方案,只是发布我的解决方案供其他人参考.使用priceToString来格式化钱.
public static String priceWithDecimal (Double price) {
DecimalFormat formatter = new DecimalFormat("###,###,###.00");
return formatter.format(price);
}
public static String priceWithoutDecimal (Double price) {
DecimalFormat formatter = new DecimalFormat("###,###,###.##");
return formatter.format(price);
}
public static String priceToString(Double price) {
String toShow = priceWithoutDecimal(price);
if (toShow.indexOf(".") > 0) {
return priceWithDecimal(price);
} else {
return priceWithoutDecimal(price);
}
}
Run Code Online (Sandbox Code Playgroud)
NumberFormat currency = NumberFormat.getCurrencyInstance();
String myCurrency = currency.format(123.5);
System.out.println(myCurrency);
Run Code Online (Sandbox Code Playgroud)
output:
$123.50
Run Code Online (Sandbox Code Playgroud)
If you want to change the currency,
NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.CHINA);
String myCurrency = currency.format(123.5);
System.out.println(myCurrency);
Run Code Online (Sandbox Code Playgroud)
output:
?123.50
Run Code Online (Sandbox Code Playgroud)
我正在使用这个(使用来自commons-lang的StringUtils):
Double qty = 1.01;
String res = String.format(Locale.GERMANY, "%.2f", qty);
String fmt = StringUtils.removeEnd(res, ",00");
Run Code Online (Sandbox Code Playgroud)
您只需要处理区域设置和相应的字符串以进行切割.
这是做到这一点的最佳方式。
public static String formatCurrency(String amount) {
DecimalFormat formatter = new DecimalFormat("###,###,##0.00");
return formatter.format(Double.parseDouble(amount));
}
Run Code Online (Sandbox Code Playgroud)
100 -> "100.00"
100.1 -> "100.10"
你应该做这样的事情:
public static void main(String[] args) {
double d1 = 100d;
double d2 = 100.1d;
print(d1);
print(d2);
}
private static void print(double d) {
String s = null;
if (Math.round(d) != d) {
s = String.format("%.2f", d);
} else {
s = String.format("%.0f", d);
}
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
打印:
100
100,10
我认为打印货币简单明了:
DecimalFormat df = new DecimalFormat("$###,###.##"); // or pattern "###,###.##$"
System.out.println(df.format(12345.678));
Run Code Online (Sandbox Code Playgroud)
产量:12,345.68美元
这个问题的可能解决方案之一:
public static void twoDecimalsOrOmit(double d) {
System.out.println(new DecimalFormat(d%1 == 0 ? "###.##" : "###.00").format(d));
}
twoDecimalsOrOmit((double) 100);
twoDecimalsOrOmit(100.1);
Run Code Online (Sandbox Code Playgroud)
输出:
100
100.10
我们通常需要做相反的操作,如果您的json money字段是浮点数,则可能为3.1,3.15或仅3。
在这种情况下,您可能需要对其进行四舍五入以使其正确显示(并稍后可以在输入字段上使用掩码):
floatvalue = 200.0; // it may be 200, 200.3 or 200.37, BigDecimal will take care
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
BigDecimal valueAsBD = BigDecimal.valueOf(value);
valueAsBD.setScale(2, BigDecimal.ROUND_HALF_UP); // add digits to match .00 pattern
System.out.println(currencyFormatter.format(amount));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
354014 次 |
最近记录: |