输入看起来像8.7000000
,我想格式化它看起来像8.70 EUR
。 我考虑过使用DecimalFormat
类:如何使用DecimalFormat格式化货币?
Double number = Double.valueOf(text);
DecimalFormat dec = new DecimalFormat("#.## EUR");
String credits = dec.format(number);
TextView tt = (TextView) findViewById(R.id.creditsView);
tt.setText(credits);
结果是8,7 EUR
。如何告诉格式化程序在,
之后有两位数字?
一般情况下,你不应该使用二进制浮点类型来存储货币值反正。使用BigDecimal或只是缩放整数(例如,具有整数美分)。 – 2011-12-21 13:49:47