gpt4 book ai didi

java - 用负数代替正数

转载 作者:行者123 更新时间:2023-11-30 06:58:44 25 4
gpt4 key购买 nike

执行此代码结束时,我期望 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(或intlong,带有缩放因子)而不是Doubledouble,后者用于 float 并且只是近似值。

关于java - 用负数代替正数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41338231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com