Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting from double to int

Status
Not open for further replies.

iSeriesCodePoet

Programmer
Jan 11, 2001
1,373
US
I am just starting out in Java and I have a question. I need to covert a variable from a double to a int. How is this done? Mike Wills
RPG Programmer

"I am bad at math because God forgot to include math.h into my programming!"

Please let us (Tek-Tips members) know if the solutions I provide are helpful to you. Not only do my posts help you but they may help others.
 
convert the double into a Double object
by
Double d1 = new Double(double)

and then use the intValue() in Double class

ie.

int newint = d1.intValue();

 
So my code should look like this then? I guess I don't quite understande yet....

public static int ConvertToF(int c)
{
double f = new Double(double);

f = ((9/5) * c) + 32;
int newint = f.intValue();
return newint;
} Mike Wills
RPG Programmer

"I am bad at math because God forgot to include math.h into my programming!"

Please let us (Tek-Tips members) know if the solutions I provide are helpful to you. Not only do my posts help you but they may help others.
 
hi Mike, pretty much there, but remember that Double is a wrapper class and so will begin with a capital D.

some minor adjustments:

public static int ConvertToF(int c)
{
Double f = new Double(((9/5) * c) + 32);
int newint = f.intValue();
return newint;
}

I think this would work as you create a new Double object by using the value returned by the calculation (which should be a double).
bruce21.jpg
 
You need a cast to convert a double to an int. Try this:

int newint = (int)f;
:)

In other words, you program should resemble this:

public class Double {

public static void main(String[] args) {
double c = 40;
double f = ((9/5) * c) + 32;
int newint = (int)f;
System.out.println(f2);
}
}

;-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
To pipk.

Uh... you don't capitalize double "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
actually the wrapper class Double does have a capital D to differentiate it from the primitive type of double. Have you not used wrapper classes yet?
bruce21.jpg
 
Nope. But the guy was asking about class double. Not the wrapper class Double. X-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
double is not a class, it is a primitive data type.

Double is a class. It is just one option open for converting from one type to another. There are also wrapper classes for all the other data types such as
Integer for int
Float[/] for float
Character[/] for char
Byte[/] for byte
Short[/] for short
Long[/] for long

They are pretty useful and have a large array of methods.

So now you know.
bruce21.jpg
 
whoops, ignore the [/], that was my bold type messing up.
bruce21.jpg
 
I'll try it and see if I can figure out what is going on. RPG is soooooooo much easier when doing something like this... Mike Wills
RPG Programmer

"I am bad at math because God forgot to include math.h into my programming!"

Please let us (Tek-Tips members) know if the solutions I provide are helpful to you. Not only do my posts help you but they may help others.
 
Ok.

That's strange, my teacher always refers to double as a class. int and String also. :-I "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
aaah come on mike, I am an ex-rpg'er and now find rpg extremely difficult, trying to remember column placings and line extension types etc. as for ile, never even had the chance to experiment.

skills now are strictly java and coolplex (CA).



bruce21.jpg
 
Hey, Koldark. I know that the code I gave you earlyer is correct. I ran it through my compiler & it worked like a charm. :-Q "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
String is a class - the easy way to differentiate is basically a class will normally have methods, such as toString() etc etc

A primitive data type stands on its own, you can only ever refer to it by its name

primitive types are basically just numbers which belong to a strict hierarchy which in top to bottom order goes like this:

double, float, long, int, short

As you rightly point out, if we want the result of an operation to be moved into a small er type e.g. double to float/long/int or short, or perhaps int to short etc, we need to use an explicit cast - but if the operation is going up the hierarchy (i.e. float to double, int to float etc) the cast is done implicitly and Java will always assume that the result will be in the higher type format. this is why when you multiply an int and a double the answer will always be converted to a double.
bruce21.jpg
 
I'm far enough into Java that I know most of that. Although it's nice to have someone explain it to me compleatly. :) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
To GT500FOMOCO,
Pipk is absolutely right. If your teacher is referring to int and double as classes than he/she is wrong. String is a class. Everything in Java is a class with the exception of the primitive types (byte, short, char int, long, float, double, and boolean). Moreover, each primitive has an associated object wrapper class to allow for OO Design (ex. try adding a int to a List, can't happen).

Also, even though it is extremely obvious I am surprised this hasn't been mentioned but you are definitely going to lose precision when casting from a double to an int. Wushutwist
 
Duh, of course you will loose persision when casting from double to int. That's a given. X-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
expalining these things helps me understand them better too. I know exactly how koldark is feeling about Java, as I have pretty much the same background as him and only been learning Java for the last year at college. I know how hard it is to get your head around OO languages coming from a structured background. But once you have the right head on, everything starts to fall into place.
bruce21.jpg
 
I know about the precision...but this is just to display the convertion from celcius for a homework assignment. My instructor went briefly over it in class, I expected to figure it out, but couldn't.

This class is basically teaching the syntax not programming, so she didn't go over it in detail, plus that might be comming up for her to talk about further.

Thanks for all the help everyone! Mike Wills
RPG Programmer

"I am bad at math because God forgot to include math.h into my programming!"

Please let us (Tek-Tips members) know if the solutions I provide are helpful to you. Not only do my posts help you but they may help others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top