我需要格式化雙倍「amt」作爲美元金額println(「$」+美元+「。」+美分),以便在小數點後有兩位數字。Java - 格式雙值作爲美元金額
這樣做最好的方法是什麼?
if (payOrCharge <= 1)
{
System.out.println("Please enter the payment amount:");
double amt = keyboard.nextDouble();
cOne.makePayment(amt);
System.out.println("-------------------------------");
System.out.println("The original balance is " + cardBalance + ".");
System.out.println("You made a payment in the amount of " + amt + ".");
System.out.println("The new balance is " + (cardBalance - amt) + ".");
}
else if (payOrCharge >= 2)
{
System.out.println("Please enter the charged amount:");
double amt = keyboard.nextDouble();
cOne.addCharge(amt);
System.out.println("-------------------------------");
System.out.println("The original balance is $" + cardBalance + ".");
System.out.println("You added a charge in the amount of " + amt + ".");
System.out.println("The new balance is " + (cardBalance + amt) + ".");
}
看**的String.format()**,順便雙是不是一個好型保持貨幣金額,使用的BigDecimal代替。 –
重複? - http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java – mawaldne