Sign in to follow this  

Need help making a calculation in Java - Basic

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 this post

Link to post
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

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

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this