亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Java 從方法中轉換為貨幣

使用 Java 從方法中轉換為貨幣

Helenr 2022-10-26 16:01:24
我正在開發一個需要 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 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
查看完整描述

3 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

您使用不正確。嘗試這個。


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 來表示貨幣?


查看完整回答
反對 回復 2022-10-26
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

-format()方法返回 a String,所以你的第一種方法已經很好了:

String currency = NumberFormat.getCurrencyInstance().format(TotalPrice);

但是,現在您的方法calculateTotalPrice()想要返回 a double,因此您需要將方法的返回類型也更改為String,或者將您的字符串轉換回 a double(我懷疑您是否真的想要)。請注意,double用于金錢問題是不好的。你應該BigDecimal改用。


查看完整回答
反對 回復 2022-10-26
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

首先,在處理財務時,您應該謹慎選擇要使用的類型。浮點數會有舍入誤差。這就是您應該使用BigDecimal的原因。

然后,要將 String 解析為 BigDecimal,如此處所建議的,您應該在 DecimalFormat 類中使用setParseBigDecimal ,然后parse將為您返回一個 BigDecimal。


查看完整回答
反對 回復 2022-10-26
  • 3 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號