This week's book giveaway is in the Android forum.We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!See this thread for details.
Win a copy of Head First Android this week in the Android forum!
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
• Paul Clapham
• Ron McLeod
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# Convert Decimal to IEEE 754 number

Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
I need to convert a decimal number to IEEE 754 half , single, Double precision numbers

code for single what I did is as follows

Marshal
Posts: 74354
334
• Number of slices to send:
Optional 'thank-you' note:
My, what a complicated solution. I work on the assumption that anything that complicated must be wrong, even without reading it.

You already have an IEEE754 number in the form of that float. You can’t convert it to an IEEE754 number, becuase it already is that. I think you mean display the bit pattern.
The floatToIntBits() method will give the 32 bits as an int, and you can get the bit pattern from that with its toBinaryString() method. Now all you have to do is pad the String with 0s from the left until it is 32 characters long.
Alternatively, set up a StringBuilder with "00000000000000000" (but 32 0s), and replace from the right with your String.

Getting a double in 64 bits is similar. Don’t know about 16 bits.

Bartender
Posts: 10780
71
• Number of slices to send:
Optional 'thank-you' note:

Laxmikant Kumbhare wrote:I need to convert a decimal number to IEEE 754 half , single, Double precision numbers

Campbell's quite right; you're overthinking this. float and double are IEEE-754 compliant floating point numbers,
and their 'twin' classes Float and Double contain methods to parse decimal numbers passed as Strings.

For half-precision, I'd have a look at BigDecimal and MathContext. I've never tried it, but I'm pretty sure it'll do 16-bit precision
for you, but whether or not it's 754-compliant you'll have to read the documentation to find out.

Winston

BTW - Please don't put such long lines in your code. It makes things awfully annoying.

Laxmikant Kumbhare
Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
THANK YOU SO MUCH

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?