MCPcopy Index your code
hub / github.com/csev/py4e / approx

Function approx

code/graphics/graphics.py:972–982  ·  view source on GitHub ↗

Returns a fixed point string approximating the number x, limiting the number of places past the decimal point if there would be more than digitCutoff significant digits, and removing unneeded trailing '0's and '.'. Assuming window dimensions under 10**digitCutoff pixels, and at lea

(x, digitCutoff = 5)

Source from the content-addressed store, hash-verified

970 return "#%02x%02x%02x" % (r,g,b)
971
972def approx(x, digitCutoff = 5):
973 """ Returns a fixed point string approximating the number x, limiting
974 the number of places past the decimal point if there would be more than
975 digitCutoff significant digits, and removing unneeded trailing '0's and '.'.
976 Assuming window dimensions under 10**digitCutoff pixels, and at least some
977 legal world coordinate having magnitude at least 1, this system should
978 provide concise approximations that distinguish all pixel coordinates.
979 """
980 fracDig = max(0, digitCutoff - len(str(int(abs(x))).lstrip('0')))
981 format = "%%.%df" % fracDig
982 return (format % x).rstrip('0').rstrip('.')
983
984def test():
985 win = GraphWin()

Callers 2

coordStrMethod · 0.85
__str__Method · 0.85

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected