This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how we convert long into float

 
rashid khokhar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ranches,

how we convert larg size of data type like long into small size of data type like float(long==>float) pls explain,read a code of my question.
------------------
 
Bob Lancaster
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rashid khokhar:
hi ranches,

how we convert larg size of data type like long into small size of data type like float(long==>float) pls explain,read a code of my question.


Simple. Just use an explicit cast. For example:
long longValue = 1234567890L;
float floatValue = (float)longValue;

The explicit cast informs the compiler you are taking responsibility for any loss of data.
See chapter 4 of RHE for a more complete explaination.

-Bob Lancaster
SCJP working on SCJD
 
Perryn Hodge
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually long to float is an automatic promotion, even though some precision may be lost.
so the following will also compile...
long longValue = 1234567890L;
float floatValue = longValue;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic