This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

Asking for Help: How can I add or substract two to the last digit of a float?

How can I add or substract two to the last digit of a float? Like 4.9999 should be 5.0001 or 4.9997. The problem is

>>> 4.9999
4.9999000000000002
would yield 4.9999000000000000 or 4.9999000000000004 which is not right.
I could make a str(4.9999), convert each character to an int and work on each character from right   
to left, but there must be a better way.
this is not what I want as well:
>>> ( 4.9999 * 10000 + 2 ) / 10000.0
5.0000999999999998 -> this should be 5.0001
this makes it somehow :))
>>> str(( 4.9999 * 10000 + 2 ) / 10000)
'5.0001'

Answer: http://www.python.org/doc/current/library/decimal.html?highlight=round#decimal-faq


CategoryAskingForHelp CategoryAskingForHelpAnswered


2026-02-14 16:06