[英]Converting to Currency from within a method using Java
我正在開發一個應用程序,需要long和double才能格式化為貨幣。 我得到正確的輸出,數字出現在我的控制台,但不是美元格式。 我嘗試使用NumberFormat,但它不起作用,也許我把它放在錯誤的地方。 這是我的代碼:
import java.text.DateFormat;
import java.util.Date;
import java.text.NumberFormat;
private Date arrivalDate;
private Date departureDate;
private long elapsedDays;
private double TotalPrice;
public static final double nightlyRate = 100.00;
public double calculateTotalPrice()
{
long arrivalDateTime = arrivalDate.getTime();
long departureDateTime = departureDate.getTime();
long elapse = departureDateTime-arrivalDateTime;
elapsedDays = elapse/(24*60*60*1000);
TotalPrice = elapsedDays * nightlyRate;
NumberFormat currency = NumberFormat.getCurrencyInstance().format(TotalPrice);
return TotalPrice;
}
public double getTotalPrice()
{
this.calculateTotalPrice();
return TotalPrice;
}
這告訴我將貨幣轉換為字符串。 當我這樣做,並嘗試返回calculateTotalPrice方法的貨幣時,java告訴我要么:將方法返回類型更改為String或將貨幣類型更改為double,這兩者都會添加更多錯誤 - 永遠不會結束循環。
我想做的就是將nightlyRate和TotalPrice更改為貨幣格式。 任何幫助我們的贊賞。
每個請求我將顯示確切的錯誤消息:當我在calculateTotalPrice()中運行這些代碼行時:
double currency = NumberFormat.getCurrencyInstance().format(TotalPrice);
return currency;
錯誤:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to double
at
calculate.reservation.totals.Reservation.calculateTotalPrice(Reservation.java:48)
at calculate.reservation.totals.Reservation.getTotalPrice(Reservation.java:54)
at calculate.reservation.totals.CalculateReservationTotals.main(CalculateReservationTotals.java:63)
當我將貨幣變量轉換為String時
String currency = NumberFormat.getCurrencyInstance().format(TotalPrice);
return currency;
我得到完全相同的錯誤說明:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from String to double
您使用不正確。 嘗試這個。
import java.text.*;
public class Currency {
private double TotalPrice;
// only one instance required
NumberFormat nformat = NumberFormat.getCurrencyInstance();
public static void main(String[] args) {
new Currency().start();
}
public void start() {
final double nightlyRate = 100.00;
int elapse = 1020202220; // random value
double elapsedDays = elapse / (24 * 60 * 60 * 1000.);
TotalPrice = elapsedDays * nightlyRate;
String formattedCurrency = formatCurrency(TotalPrice);
System.out.println(formattedCurrency);
}
public String formatCurrency(double amount) {
String fmtedCurrency = nformat.format(amount);
return fmtedCurrency;
}
}
根據您的具體情況,您可能需要也可能不需要設置區域設置。 你應該用美分來處理你的貨幣單位。 稍后轉換為美元。 這可能對你有用。 為什么不使用Double或Float來表示貨幣?
format()
-method返回一個String
,所以你的第一個方法已經非常好了:
String currency = NumberFormat.getCurrencyInstance().format(TotalPrice);
但是,現在你的方法calculateTotalPrice()
想要返回一個double
,所以你需要將方法的返回類型更改為String
,或者將你的字符串轉換回double
(我懷疑你真的想要)。 請注意,使用double
的貨幣問題是不好的。 你應該使用BigDecimal
。
首先,您應該仔細選擇處理財務時使用的類型 。 浮點數將具有舍入誤差。 這就是你應該使用BigDecimal的原因。
然后,要將String解析為BigDecimal, 如此處所示 ,您應該在DecimalFormat類中使用setParseBigDecimal ,然后parse將為您返回一個BigDecimal。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.