Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting between float <-> int in javascript

I have a javascript program (running in jsdb which uses Mozilla Spidermonkey 1.8, not in a browser) that I need to convert float and doubles to/from the appropriate number of bytes of their IEEE representations. Java has Float.floatToIntBits() and Float.intBitsToFloat() and similar methods for Double.

Is there a way to compute these functions in Javascript? (and yes, I know that all javascript numbers are essentially of type double)

I guess I could follow the algorithms in the javadoc for Double.longBitsToDouble() and Float.intBitsToFloat() but that only covers one direction, and I need both.

like image 246
Jason S Avatar asked May 07 '26 22:05

Jason S


1 Answers

javascript float from/to bits

From the answer: IEEE-754 Floating-Point Conversion

like image 177
JaredMcAteer Avatar answered May 10 '26 12:05

JaredMcAteer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!