Converting floating point number to any base
floating-point, floating-point-conversion, python
Solution
I think your idea was quite good already. To implement it, you would first need a dictionary that can convert strings to integers. Then you'd want to split your string into two, at the decimal point. Then, you could reverse your "before" string and iterate through it, multiplying the integer of the current value with 10 to the current index and adding all these values up.
Then, iterate through your "after" string and multiply the current value with the negative current index, adding the values again.
To put this into code:
s2i = {"0": 0,
"1": 1,
"2": 2,
...
"Y": 34,
"Z": 35
}
def convert_float(s, base=10):
ret = 0
if "." not in s: bef = s
else: bef, aft = s.split(".")
for i in enumerate(reversed(bef)):
integer = s2i[i[1]]
if integer >= base: raise ValueError
ret += base**i[0] * integer
if "." not in s: return ret
for i in enumerate(aft):
integer = s2i[i[1]]
if integer >= base: raise ValueError
ret += base**-(i[0] + 1) * integer
return ret
print convert_float("YF.1G90N", 36)
> 1239.04031674
Problem
How would i convert a float, represented by a string, to a decimal, base between 2 to 36 in Python, without using Python built ins int and float? meaning: convert_float("234.56", base) --> float, or ("10AB", base) --> float In case that the float ends in .0, the result should be an integer. Converting integers to any base seems much less complicated, however I couldn't come up or find any solution for the floats.