第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用 Java 從方法中轉(zhuǎn)換為貨幣

使用 Java 從方法中轉(zhuǎn)換為貨幣

Helenr 2022-10-26 16:01:24
我正在開發(fā)一個需要 long 和 double 才能格式化為貨幣的應(yīng)用程序。我得到了正確的輸出,數(shù)字出現(xiàn)在我的控制臺中,但不是美元格式。我試過使用 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; }這告訴我將貨幣轉(zhuǎn)換為字符串。當(dāng)我這樣做并嘗試為 calculateTotalPrice 方法返回貨幣時(shí),java 告訴我:將方法返回類型更改為 String 或?qū)⒇泿蓬愋透臑?double,這兩者都會增加更多錯誤 - 永無止境的循環(huán)。我要做的就是將 nightlyRate 和 TotalPrice 更改為貨幣格式。對我們的任何幫助表示贊賞。根據(jù)請求,我將顯示確切的錯誤消息:當(dāng)我在 calculateTotalPrice() 中運(yùn)行這些代碼行時(shí):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)當(dāng)我將貨幣變量轉(zhuǎn)換為字符串時(shí)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貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超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;

   }


}

根據(jù)您所在的位置,您可能需要也可能不需要設(shè)置語言環(huán)境。你應(yīng)該使用美分來處理你的貨幣單位。稍后兌換成美元。這可能對你有用。為什么不使用 Double 或 Float 來表示貨幣?


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

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個贊

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

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

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


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

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個贊

首先,在處理財(cái)務(wù)時(shí),您應(yīng)該謹(jǐn)慎選擇要使用的類型。浮點(diǎn)數(shù)會有舍入誤差。這就是您應(yīng)該使用BigDecimal的原因。

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


查看完整回答
反對 回復(fù) 2022-10-26
  • 3 回答
  • 0 關(guān)注
  • 242 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號