• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Float to Int(VERY URGENT)

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How to convert Float to Int value? Plz explain me by an example?
Thanks,
Angela
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You perform a "cast" on it, like this:
float x;
int y;
y = (int)x;
Note that you could possibly lose information doing this, because a float is bigger than an int...
In fact, if "x" in the example above was originally 100.3D, after the cast it becomes just 100 without the fractional portion.
[This message has been edited by John Lynn (edited April 03, 2001).]
[This message has been edited by John Lynn (edited April 03, 2001).]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean Float to int, float to int , or Float to Integer?
Java is case sensitive, so it makes a difference.
Howard
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,

Originally posted by John Lynn:
You perform a "cast" on it, like this:
float x;
int y;
y = (int)x;
Note that you could possibly lose information doing this, because a float is bigger than an int...
In fact, if "x" in the example above was originally 100.3D, after the cast it becomes just 100 without the fractional portion.
[This message has been edited by John Lynn (edited April 03, 2001).]
[This message has been edited by John Lynn (edited April 03, 2001).]


 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float to int

Originally posted by Angela Jessi:
Thanks John,


 
Ranch Hand
Posts: 1479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its true you lose the decimal value in the conversion, and since you're converting to int that probably isn't an issue, but be aware that the float value you start with may not be accurate for large numbers and int variables store large int values more accurately. This could be an issue with large numbers or medium size numbers where there are lots of conversions and accuracy is required. The code below shows how float does not store large non decimal values accurately :

class c{
{ public static void main(String ja[])
int x = 312345678;
float y = 312345678f;
System.out.println(x );
System.out.println(y );

}
}
The output is :
312345678 // int value
3.12345664E8 // float value
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic