Welcome to the iTechForums.
If this is your first visit, be sure to check out the
FAQ by clicking the link above.
You may have to
register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
|
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 3
Members:
Number of Users Online:
Welcome to our newest member, |
|
 |

05-15-2008, 02:34 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 2
|
|
Converting data types in Java?
I wondered if it is possible to convert a "double" data type into a "integer" data type?
This is simply because I have calculated a formula which is giving me an answer in a "double" data type... and when i output the result I basically just want to knock off the decimal point and give the answer as an integer.
Doing this the way I would do it as a beginner at java just gives me the error, possible loss of precision.
I understand you may not be able to do it this way, and if its so its cool.
would appreciate it if people could give me some help.
Thanks in advance.
|

05-15-2008, 02:39 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 20
|
|
Get real familiar with the
String.format()
method. Consider...
double dd = 555.5554;
System.out.println( String.format("double to int is: %3.0f", dd));
and the result is: 556 -- as a String
for a real int...
double dd = 555.5554;
int x = Integer.parseInt( String.format("%3.0f", dd) ); // this method uses String
And, what's going on? This is like the C language.
% = entity
3.0 = number of columns, number of decimals
f = a floating point number, either float or double
Because, in hacking we have to jump back and forth between String and a number and back again. String.format() has been the fastest technique I have learned from Java 1.5+.
|

05-15-2008, 02:43 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 8
|
|
you can do this;
(int)(a*b);
where a * b is returning a double.. the (int) will convert it to an integer.
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
All times are GMT. The time now is 09:21 AM.