Hey guys, in my programming assignment I need to split the mantissa and exponent and add them to two separate variables. so £1.88 would be

double amount = 1.88;
//then after the conversion
mantissa = 1;
exponent = 88;

I've looked at this a lot over the web, and I can only find extremely complicated tutorials, I've only started learning java so I don't want to have to deal with anything complicated at the moment and I'd rather work may way up to that stuff.

if anyone could point me in the direction of a decent thread, or a decent website with tutorials and source code. I have researched but to no avail :(

Cheers guys.

If you want to do it with the math library, I would try using math.floor() - look it up in the documentation for java.math. That'll get you the int part, and you can easily get the fractional part from there.

Or you could remove the fractional part by casting the float to an int, which has the same effect. In either case, though, you should verify that your solution works for negative numbers before you turn it in.

If you want to do it using your naked wits, then you've got an interesting challenge. One simpleminded approach would be (assuming the number is positive) to subtract 1 until the number goes negative - then you know how many 1s the number had in it (the magnitude of the integer part) and the fractional part is easily recovered. That's a bit brute-force-ish, but maybe you can improve on it. :)

(btw, the terms "mantissa" and "exponent" don't mean quite that - "mantissa" would be 1.88, exponent would be 0 in your case. if the number were 188, then mantissa would be 1.88, exponent would be 2)

Jon gave some great advice that you should definitely look into to. Another way to go about it may be to convert it to a string, then manipulate that. You can loop through each character of the string of numbers and build another string based on everything before the dot, and yet another string based on everything after the dot. You could always convert the strings back to ints if you need to do any arithmetic with them, or leave them as strings if all you have to do is display them. A for loop using the String class's charAt() method along with StringBuilder ought to do the job nicely.

Good Luck

See? There's always more than one way to do it.

Quick contest: whoever comes up with the most, um, ingenious solution in the next three hours gets two reputation points. :)
(meaning ingenious in the Rube Goldberg sense, of course!)

Jon said something v important - either you do not want mantissa/exponent or your example is completely wrong. You need to check the specs for this project before going any further

If it really is mantissa/exponent then take log to base 2 and floor that to int - that gives the exponent. Divide by 2^exponent to get the manissa.
psJon re your example - shouldn't the exponent be a power of 2 not a power of 10?

Edited 6 Years Ago by JamesCherrill: n/a

If you're thinking logs, yes, you're right. "Mantissa" is also the term that I've always heard for the "significand" of a floating-point number, which is what I was thinking. Wikipedia says "significand" is the preferred term for that usage, though - there I go learning something new.

This article has been dead for over six months. Start a new discussion instead.