- 48 you would be better off using - '0' as this will work regardless of the executable character set in use.On to how to do it, assuming that the number is in range for an integer then, as Nepomuk says you can do the bit to the left of the decimal point by converting to an integer and using the modulus (%) and division operators to separate out individual digits.
After that doing the fractional part, right of the decimal point, I think you are going to need an idea of how many digits you want to include in the calculation, the issue being if you think think of you floating point number 12.56 the computer could be holding either 12.559999999999 or 12.560000000001 . In either case you are actually only interested in the value to 2 digits.
If the integer part has been dealt with then we are left with either 0.559999999999 or 0.560000000001. But if we know we want only 2 decimal places we can times by 10^2 (100) add 0.5 and convert to an integer.
[code]
0.559999999999 0.560000000001
*100 55.9999999999 56.0000000001
+0.5 56.4999999999 56.5000000001
convert to int 56 56
And then repeat the procedure that we used on the initial integer part. In fact in we know that the size of the original floating point number is small enough then we can do this bit first and end up with an integer containing 1256.
Another option would be to put it in a string and then process the characters of the string as stdq suggested.
The main limitation of both of these is that you need to know how many decimal places are in floating point number you wish to convert and that may not be easy to work out.
Some pseudo code like this might work
Code:
tolerance = 0.0000001
number = approx(12.56)
number = absolute_value(number)
places = 0;
stop = false
while((!stop) && (places != 15))
{
high = number - integer(number)
low = (integer(number) + 1) - number
if ((high < tolerance) || (low < tolerance))
{
stop = true
}
else
{
number = number - integer(number)
number = number * 10
places = places + 1
}
}
Leave a comment: