Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > doubles and integers

Reply
Thread Tools

doubles and integers

 
 
Paul Morrison
Guest
Posts: n/a
 
      01-04-2005
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


 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      01-04-2005
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

 
Reply With Quote
 
 
 
 
Ryan Stewart
Guest
Posts: n/a
 
      01-04-2005
<> wrote in message
news: oups.com...
> 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?


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mixing doubles and integers? Johs C Programming 3 01-03-2007 03:04 PM
how to count rows and columns of integers/doubles in a file? Martin Joergensen C Programming 68 04-03-2006 02:11 AM
Problems with multiplications of doubles and/or floats J.K. Becker C++ 43 04-23-2004 10:41 PM
floats doubles long doubles dan C++ 1 11-26-2003 05:12 AM
unions with long long ints and doubles? Chris N. Hinds C Programming 3 10-01-2003 11:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57