java - Infinity number returns a NumberFormatException even after casting to two decimal places -
i have researched , tried different solutions fix problem keep getting same exception.
i have class called balancebreakdownfee
public class balancebreakdownfee { private double amount; private double balance; public double getamount() { return amount; } public void setamount(double amount) { this.amount = amount; } public double getbalance() { return balance; } public void setbalance(double balance) { this.balance = balance; } }
then have method populates amounts list.
private void addquarterlyfees(){ list<period> periods = getpaymenttermperiods(); int discountpercentage = getdiscountpercentage(); double perquarteramount = getperperiodamount(); double perquarterbalance = perquarteramount; balancebreakdownfee dp = getdownpayment(); if (discountpercentage >= 100) { dp.setamount(0); dp.setbalance(0); breakdown.add(dp); } else { breakdown.add(dp); } (period p : periods) { balancebreakdownfee b = new balancebreakdownfee(); b.setdescription(p.getdescription()); b.setamount(perquarteramount); if(double.isinfinite(perquarteramount)){ double number = perquarteramount; number = math.round(number * 100); number = number / 100; system.out.println("@addquarterlyfees : "+p.getdescription()+" : "+perquarteramount); system.out.println("@quarterlyfees casted: "+number); system.out.println("@addquarterlyfees rounded :"+bigdecimal.valueof(perquarteramount).setscale(2,roundingmode.ceiling)); } b.setbalance(perquarterbalance); b.setdeadline(p.getdeadlineofpayment()); breakdown.add(b); } }
when run program display values through system.out.println()
, console.
@addquarterlyfees : first quarter : infinity @quarterlyfees casted: 9.223372036854776e16 exception in thread "awt-eventqueue-0" java.lang.numberformatexception
i need 2 decimal places since program i'm writing processing fees involves populating of fees , applying discounts.
how can fix this? have tried , researched hours.
to add, need store values mysql database has table column set decimal(10,2)
if change datatype of balancebreakdownfee
fields amount
, balance
bigdecimal
, worry might return bigger numbers
i tried use decimalformat
seems it's better use when hiding trailing zeros not converting infinity
value actual double
2 decimal places.
then in dao implementation have ff.
cs_addbalbreakdownfee.setstring(1, b.getdescription()); cs_addbalbreakdownfee.setdouble(2, b.getamount()); cs_addbalbreakdownfee.registeroutparameter(3, types.integer); cs_addbalbreakdownfee.executeupdate(); int balancebreakdownfeeid = cs_addbalbreakdownfee.getint(3);
the actual jtable
holding record shows double values correct precision used decimalformat
.
however, doesn't fix problem infinity
during insert of data mysql.
i'd appreciate suggestion.
thank you.
wiki
Comments
Post a Comment