Win a copy of The Java Performance Companion this week in the Performance 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