doubles and integers

P

Paul Morrison

I have the following code:

m = Math.ceil(((Math.log(R) - Math.log(P*(1-J) + R))/(Math.log(J))));

It is to work out the number of months it will take to pay back a loan with
3 givens. The problem is that m is months, and must be an integer, whereas
the ceil method returns a double. How do I get around this, I have looked
for toInt or toInteger methods, but cant find anything like it.

Thanks for your time

Paul Morrison
 
K

klynn47

The only way to turn a double into an int is with an explit cast. The
syntax is

int num = (int)d; where d is a double
 
R

Ryan Stewart

The only way to turn a double into an int is with an explit cast. The
syntax is

int num = (int)d; where d is a double
First, that's not true. There's also the intValue method of Double. Second,
both this method and yours truncate the result. A better solution would be:
int num = (int) (d + 0.5);

Or there's always Math.round if you can use a long.

Also, klynn, will you please start quoting posts to which you are replying?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top