I need to convert the result of a calculation from type double to type int, what's the best way of going about this?
I need to do this because I need the results of the calculation to be the length of a rectangle that will be drawn.
Many thanks
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I need to convert the result of a calculation from type double to type int, what's the best way of going about this?
I need to do this because I need the results of the calculation to be the length of a rectangle that will be drawn.
Many thanks
You can cast it: int i = (int)1.3;
Ok I think I got that, but BlueJ is saying that I'm having an error still.
import java.awt.*; import javax.swing.*; public class goldenrect extends JApplet { double length; public void init() { String shortSide; double shortnumb; shortSide = JOptionPane.showInputDialog ("Enter the length of the shorter side in pixels"); shortnumb = Integer.parseInt( shortSide ); length = shortnumb * 1.6180339887; int shortnumb2 = (int) shortnumb; int length2 = (int) length; } public void paint ( Graphics g ) { g.drawRect( 15, 10, length2, shortnumb2 ); g.drawString( "The length of the longer side is " + length, 25, 25); } }
It says that it cannot find the symbol- variable length2
Any ideas? Any help is greatly appreciated
What line is the error on?
Is length2 a local variable in a method?
or is it a class variable know to all methods?
Local variables are only known in the method (or enclosing {}s) they are defined in.
rph (July 21st, 2011)
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
Yeah that's what the problem was, I hadn't declared length2 as a class variable, thanks
However now I have the problem that the conversion from double to int makes the int value 0, any ideas why this might be?
import java.awt.*;
import javax.swing.*;
public class goldenrect extends JApplet
{
double length, shortnumb;
int length2, shortnumb2;
String shortSide;
public void init()
{
String shortSide;
double shortnumb;
shortSide =
JOptionPane.showInputDialog ("Enter the length of the shorter side in pixels");
shortnumb = Double.parseDouble( shortSide );
length = shortnumb * 1.6180339887;
int shortnumb2 = (int) shortnumb;
int length2 = (int) length;
}
public void paint ( Graphics g )
{
g.drawRect( 15, 10, length2, shortnumb2 );
g.drawString( "The length of the longer side is " + length, 25, 25);
}
}
Never mind I've figured it out myself, I just had to move
int shortnumb2 = (int) shortnumb;
int length2 = (int) length;
into the public void paint class.
Thanks for all your help
you can parse.. or change data type into INT.. or cast