Public Group

# Need help making a calculation in Java - Basic

This topic is 4626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I have a problem in that I want a result of an IF statement I've made to perform based on the amount of credit available (to make a call) So that if the credit available is not sufficient enough to pay for the call of an entered duration (at high and low rates - depending on call duration) - if(Duration < 120 && CreditAvailable < (Duration * hRate)) { CreditUsed = Duration * hRate CreditAvailable = CreditAvailable - CreditUsed; CreditUsed = 0; - As you can see from the above this calculates the value of credit used, but I want it to change the value of duration based on the amount of credit available. I've been trying to change the type of duration to double to make it possible to make a equation to do so but keep getting the possible lack of precision error (Duration is currently INT)

##### Share on other sites
Try this:

if(Duration < 120 && CreditAvailable < (((float)Duration) * ((float)hRate)))

##### Share on other sites
This might sound crasy... but its not your int thats the problem... but your double's...

Double and Float values are both floating-point values which use base two, this means they cannot store decimal fractions exactly, resulting in rounding errors and a loss of precision.

so a double value is not as presice as an integer... thats proberly why u get that message...

but as H_o_p_s posted, typecasting might solve your problem... but then u have an idea why u get that error...

You can also consider using a BigDecimal instead, since BigDecimals are exact representations of decimal fractions, and couses no precision loss...

and further u can take a look at
http://www.roguewave.com/support/docs/hppdocs/mnyapi/com.roguewave.money.currency.v1-0.BigDecimalValue.html

which i (almost) just stumbled on, which aparently provides a bit more...

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 10
• 11
• 37
• 12
• 12
• ### Forum Statistics

• Total Topics
631414
• Total Posts
2999958
×