我需要输出e4和e5到2位小数,因为它是货币
public void afterTextChanged(Editable s) {
if (e2.getText().toString() != "")
{
double salePrice = Double.parseDouble(e2.getText().toString());
double ebayFee = salePrice / 10.00;
double paypalFee = (salePrice * 0.034) + 0.2;
double roundedPPFee = Math.round(paypalFee*100.00)/100.00;
double roundedEbayFee = Math.round(ebayFee*100.00)/100.00;
e4.setText(String.valueOf(roundedEbayFee));
e5.setText(String.valueOf(roundedPPFee));
}
}
答案 0 :(得分:1)
您可以使用DecimalFormat(可能使用RoundingMode)
//double yourNumber = ...;
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.FLOOR);
String roundedValue = df.format(yourNumber);
//double yourNumber = ...;
String roundedValue = BigDecimal.valueOf(yourNumber).setScale(2, BigDecimal.ROUND_FLOOR).toString();
您可能需要查看类似问题得到广泛回答的the other thread。
另一方面,由于无法准确表示小数值,您可能不应该将钱存入双倍 - 请参阅this question以获得更广泛的解释。
答案 1 :(得分:0)
您目前的输出是什么?
也试试这个并检查你的输出。而不是.00,只需添加.0
double roundedPPFee = Math.round(paypalFee*100.0)/100.0;
double roundedEbayFee = Math.round(ebayFee*100.0)/100.0;
答案 2 :(得分:0)
double ebayFee = salePrice / 10.00;
double paypalFee = (salePrice * 0.034) + 0.2;
double roundedPPFee = Math.round(paypalFee*100.0)/100.0;
double roundedEbayFee = Math.round(ebayFee*100.0)/100.0;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(roundedPPFee));
System.out.println(f.format(roundedEbayFee));