在执行此代码的最后,我希望为0.00,但得到-0.00。
该代码根据市场利率计算债券价格,确定
如果以溢价或折扣提供,则进行计算。
我所关心的部分是确定保费余额
帐户。在计算此值时,我对变量premium_bal2使用-=。
这是代码:
package totalbeginner.demo;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
public class TotalBeginner{
public static void main(String[] args){
System.out.println("We are offering a $100,000 bond at 10% par for 3 years");
double price; //price of bond determined from rates
double market_rate = Double.parseDouble(JOptionPane.showInputDialog("What is the market rate?"));
NumberFormat formatter= NumberFormat.getCurrencyInstance(Locale.UK);
int year=1;//year
double premium_bal=0;;//premium balance
double premium_bal2=0;
if(market_rate<10){//premium bond offer
price = (0.10*100000*5.5673)+(100000*0.5565);
premium_bal= price-100000;//premium bal
premium_bal2 = price-100000;//premium bal
System.out.println("Cash dr. "+formatter.format(price));
System.out.println("Premium dr. "+formatter.format(price-100000));
System.out.println("Bonds Payable cr. "+ formatter.format(100000));// issue journal entry
while(year<3){
System.out.println("Interest dr. "+formatter.format(10000));//interest payment
System.out.println("Cash cr. "+formatter.format(10000));
//write-off
System.out.println("Interest dr. "+formatter.format(premium_bal/3));
System.out.println("Premium cr. "+formatter.format(premium_bal/3));
premium_bal2-=premium_bal/3;
if (year==2){
System.out.println("Interest dr. "+formatter.format(10000));//interest payment
System.out.println("Cash cr. "+formatter.format(10000));
System.out.println("Cash cr. "+formatter.format(100000));//Bond retirement
System.out.println("Bonds Payable dr. "+formatter.format(100000));
//write-off
System.out.println("Interest dr. "+formatter.format(premium_bal/3));
System.out.println("Premium dr. "+formatter.format(premium_bal/3));
//Amount in balance account
premium_bal2-=premium_bal/3;
System.out.println("Amount in premium account: "+formatter.format(premium_bal2));
}
//counter year
year++;
}
}
System.exit(0);
}
}
最佳答案
使用BigDecimal
(或带有缩放因子的int
或long
)代替Double
或double
,后者用于浮点数,仅是近似值。
关于java - 负数代替正数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41338231/