I’m fairly new to Python, and was wondering how would I be able to control the decimal precision of any given number without using any the decimal module or floating points (eg: ” %4f” %n).
Examples (edit):
input(2/7)
0.28571428571….
input(1/3)
0.33333333333333….
and I wanted them to thousand decimal points or any decimal point for that matter. I was thinking of using a while as a controlled loop, but I’m not really sure how to do so. Thanks
edit: The reason why I’m not using the decimal module is just so I can conceptualize the algorithm/logic behind these type of things. Just trying to really understand the logic behind things.
Without the Decimal module (why, though?), assuming Python 3:
Thanks to @nneonneo for the clever
.zfill()idea!Caveat: This uses floor division, so
divide(2,3,2)will give you0.66instead of0.67.