Floating Point = 11.0000123456789
I want to round the above floating point number to 11.00001235. So I want the base portion of the float rounded to 4 digits while ignoring and preserving the leading zeros and adding back in the significand at the end.
I have the following, it is short and sweet but feels a little bit like a work around.
import decimal
decimal.getcontext().prec = 4
significand, base = str(11.0000123456789).split('.')
fp = significand + str(decimal.Decimal('.' + base) + 0)[1:] # I need to add 0 here for it to work
print(fp)
I can't really find an answer to my specific question. I want to know what the most pythonic way of achieving this is or if what I have is decent enough. It feels a little shoddy to me.
Edit: the number of leading zeros is unknown and I chose four for the example. So I don't think string formatting will work.