我被要求实现一个 printDailyCost
方法,该方法应该调用 getDailyCost 方法并将返回的值格式化为 2
小数位。然后它应该打印这个值以及一个 £ 符号。
到目前为止我已经:
public abstract class Suit {
private String colour;
private double dailyCost;
private int trouserLength;
private int jacketChestSize;
private boolean available;
private double totalPrice;
public Suit(String colour, double dailyCost, int trouserLength,
int jacketChestSize, boolean available, double totalPrice) {
super();
this.colour = colour;
this.dailyCost = dailyCost;
this.trouserLength = trouserLength;
this.jacketChestSize = jacketChestSize;
this.available = available;
this.totalPrice = totalPrice;
}
public String getColour() {
return colour;
}
public double getDailyCost() {
return dailyCost;
}
public int getTrouserLength() {
return trouserLength;
}
public int getJacketChestSize() {
return jacketChestSize;
}
public boolean isAvailable() {
return available;
}
public double getTotalPrice() {
return totalPrice;
}
public void setColour(String colour) {
this.colour = colour;
}
public void setDailyCost(double dailyCost) {
this.dailyCost = dailyCost;
}
public void setTrouserLength(int trouserLength) {
this.trouserLength = trouserLength;
}
public void setJacketChestSize(int jacketChestSize) {
this.jacketChestSize = jacketChestSize;
}
public void setAvailable(boolean available) {
this.available = available;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public void calcTotalPrice(int numDaysHired){
this.totalPrice = dailyCost * numDaysHired;
}
public String printDailyCost() {
return printDailyCost();
}
}
我的问题是如何修改 printDailyCost 方法以调用 getDailyCost 方法并将返回的值格式化为 2 小数位然后用 £ 符号打印?
最佳答案
看看这个:
java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(java.util.Locale.UK);
System.out.println(format.format(getDailyCost()));
关于Java printDailyCost 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205216/