简体   繁体   中英

Store floating point number into long type

I'm using Hadoop counter in Java, which only supports integer/long counter type, but I need a floating point counter. I was wondering is there a way to store a floating point number into integer or long type, then recover the original value from integer/long type without losing precision?

Double.doubleToLongBits and Double.longBitsToDouble should do the trick if it's a double . There are analogous methods on Float for ints.

Class Float provides the methods you need:

int intValue = Float.floatToIntBits(floatValue);
int floatValue2 = Float.intToFloatBits(intValue)
// floatValue2 == floatValue

The same methods are available for double types too (see class Double )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM