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.
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.
BTW - Please don't put such long lines in your code. It makes things awfully annoying.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here