You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
Answer from Rex Logan on Stack Overflow
🌐
Stack Abuse
stackabuse.com › bytes › limiting-float-decimal-points-in-python
Limiting Float Decimal Points in Python
August 31, 2023 - In this example, we first import the Decimal module. We then convert our float number to a Decimal and use the round() function to limit the decimal points to two. The Decimal module provides more precision and control over floating point arithmetic than the built-in Python float data type.
Top answer
1 of 16
2333

You are running into the old problem with floating point numbers that not all numbers can be represented exactly. The command line is just showing you the full floating point form from memory.

With floating point representation, your rounded version is the same number. Since computers are binary, they store floating point numbers as an integer and then divide it by a power of two so 13.95 will be represented in a similar fashion to 125650429603636838/(2**53).

Double precision numbers have 53 bits (16 digits) of precision and regular floats have 24 bits (8 digits) of precision. The floating point type in Python uses double precision to store the values.

For example,

>>> 125650429603636838/(2**53)
13.949999999999999

>>> 234042163/(2**24)
13.949999988079071

>>> a = 13.946
>>> print(a)
13.946
>>> print("%.2f" % a)
13.95
>>> round(a,2)
13.949999999999999
>>> print("%.2f" % round(a, 2))
13.95
>>> print("{:.2f}".format(a))
13.95
>>> print("{:.2f}".format(round(a, 2)))
13.95
>>> print("{:.15f}".format(round(a, 2)))
13.949999999999999

If you are after only two decimal places (to display a currency value, for example), then you have a couple of better choices:

  1. Use integers and store values in cents, not dollars and then divide by 100 to convert to dollars.
  2. Or use a fixed point number like decimal.
2 of 16
838

There are new format specifications, String Format Specification Mini-Language:

You can do the same as:

"{:.2f}".format(13.949999999999999)

Note 1: the above returns a string. In order to get as float, simply wrap with float(...):

float("{:.2f}".format(13.949999999999999))

Note 2: wrapping with float() doesn't change anything:

>>> x = 13.949999999999999999
>>> x
13.95
>>> g = float("{:.2f}".format(x))
>>> g
13.95
>>> x == g
True
>>> h = round(x, 2)
>>> h
13.95
>>> x == h
True
Discussions

how do I limit the amount of decimal values stored in a float variable?
First, if you are not displaying it anywhere, do not do anything. There is no need to unnecessarily reduce accuracy. You are not hurting anything by having extra decimal places. But if you are and want to format it, well, that's just formatting like this https://appdividend.com/2022/06/23/how-to-format-float-values-in-python/ More on reddit.com
🌐 r/learnpython
6
1
November 20, 2022
How do I round a double to one decimal?
For surprisingly many cases, rounding a double/float in-place is probably not the right thing you actually want to do. To show it to a user / print a double / or converting it to string, use one of the following depending on how you usually handle string I/O: For printf / sprintf, use %.1f format specifier. For streams (std::cout / std::ostringstream usually), use std::setprecision https://en.cppreference.com/w/cpp/io/manip/setprecision For std::format, use {.1f} format specifier. When the result value has to be up to x decimal digits, consider using integers instead of doubles for result values: int i = static_cast(d * 10 + 0.5); This might not be accurate because of floating point roundings and inaccuracies... When the result of rounding has to be a floating point number, do as another comment suggests: multiply by a number by 10, round it, then divide it by 10. However, note that doing so sometimes yield incorrect values because of the nature of IEEE floating point numbers. More on reddit.com
🌐 r/cpp_questions
16
5
October 9, 2023
How do I represent currency (i.e., rounding to two decimal places) in Python? I just started learning a few days ago, and any explanation I've found of the decimal function is overwhelming at this point. ELI5, plz?

You're thinking about the wrong aspect of the problem. Yes, floating point innaccuracies mean you get a number like 30.00000000000000001, and decimal will fix that - but you can still divide a price and get a value like $3.718. Decimal won't help you there.

What you really want is a way to round the value to 2 decimal places when you print it. That's the only time that it matters, unless you're a bank and you really care about tiny fractions of a cent (in which case you'll use decimal as well as the following advice).

Check out the format function.

price=14.6188
print("The price is: ${:.2f}".format(price))
The price is: $14.62

This function is very powerful and you should get familiar with it.

product="beer"
print("The price of {:} is ${:.2f}".format(product, price))
The price of beer is $14.62
More on reddit.com
🌐 r/learnpython
19
12
November 16, 2013
How to use the float() command to create 2 decimal places instead of one in a product of 2 numbers [the product is dollar amount]
You generally don't round until you go to display the value. You can use round for that, but since you just want to display it with two digits, you can use string formatting . In your case, something like print(f'{totalPay:.2f}'). .2f is the format specifier, saying round the float (f) to two decimal places (.2). More on reddit.com
🌐 r/learnpython
9
7
January 28, 2020
🌐
Python Engineer
python-engineer.com › posts › limit-float-python
How to limit float values to N decimal places in Python - Python Engineer
June 20, 2022 - A handy trick using format specifiers to limit float values. Pratik Choudhari · · · June 20, 2022 · 1 min read · A float data type is a numeric data type with decimal places. In some cases, float values can go to hundreds of decimal places.
🌐
Python Shiksha
python.shiksha › home › tips › 3 python ways to limit float up to two decimal places
3 Python ways to Limit float up to two decimal places - Python Shiksha
July 18, 2021 - The string along with the format() method will restrict the floating-point numbers up to 2 decimal places. ... In this article, we briefly discussed about the few different methods about how to limit floating-point numbers up to two decimal points.
🌐
PythonHow
pythonhow.com › how › limit-floats-to-two-decimal-points
Here is how to limit floats to two decimal points in Python
New: Practice Python, JavaScript & SQL with AI feedback — Try ActiveSkill free → × ... To limit a float to two decimal points in Python, you can use the round function. The round function takes a number and the number of decimal points to round to as arguments, and returns the rounded number.
🌐
Quora
quora.com › How-do-you-limit-decimal-places-in-Python
How to limit decimal places in Python - Quora
Answer: Do you mean you want to limit the decimal places when converting a float (or Decimal) to a string format? In that case, string formatting can help you out: [code]In [6]: x = 0.406 In [7]: f'{x:.2f}' Out[7]: '0.41' [/code]The [code ].2[/code] in the format specifier indicates we want 2 ...
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › how to limit floats to two decimal points in python?
How to Limit Floats to Two Decimal Points in Python? - Be on the Right Side of Change
May 31, 2022 - The precision is set to 0 digits, so round(3.14) results in 3. Syntax: round(number, precision) # rounds the number to the given precision and returns the result ... x = 3.143667 y = round(x) z = round(x, 3) # Limit the float value to three ...
🌐
Blogger
blockdmask.blogspot.com › 2025 › 06 › python-round-float-to-decimal-places.html
[Python] 3 Ways to Limit Decimal Places in Python (With Examples)
June 25, 2025 - Explanation: With f-strings, just use {value:.Nf} to limit to N decimal places. It’s fast, clean, and readable. If you’re working with money, Python’s built-in float can introduce small inaccuracies.
🌐
Tutorialdeep
tutorialdeep.com › knowhow › python faqs › how to limit float to two decimal places in python
How to Limit Float To Two Decimal Places in Python -
May 18, 2021 - learn how to limit float to two decimal places in Python. The short answer is: use the Python format() or round() to convert float to two
🌐
CodeGenes
codegenes.net › blog › how-to-limit-decimal-places-in-python
How to Limit Decimal Places in Python — codegenes.net
One common way is to use the f - strings (formatted string literals) introduced in Python 3.6. num = 2.71828 formatted_num = f"{num:.2f}" print(formatted_num) # Output: 2.72 · Here, the :.2f inside the f - string indicates that we want to format ...
🌐
Derludditus
derludditus.github.io › ClaudePython › how-to-limit-decimal-places-in-python.html
How to limit decimal places in Python
The round() function provides a straightforward way to control decimal places in Python. It takes two arguments: the number you want to round and the count of decimal places to keep. In the example, round(num, 2) rounds 3.14159 to 3.14, preserving exactly two decimal places.
🌐
Sentry
sentry.io › sentry answers › python › limit floats to two decimal points in python
Limit floats to two decimal points in Python | Sentry
March 15, 2023 - How do I round a floating point number to two decimal places? We can do this using Python’s round() function, which rounds a given number to a specified number of decimal places. For example: my_number = 9.489999999999 rounded = round(my_number, 2) print(rounded) # will output "9.49" Due to the limitations ...
🌐
W3docs
w3docs.com › python
Limiting floats to two decimal points
To limit a float to two decimal points, you can use the round() function. This function will round a number to a specified number of decimal places.
🌐
GeeksforGeeks
geeksforgeeks.org › python › precision-handling-python
Precision Handling in Python - GeeksforGeeks
December 19, 2025 - ... This method helps when calculations must follow a specific precision throughout the entire operation. By setting getcontext().prec, every Decimal calculation automatically respects that precision limit...
🌐
CodeRivers
coderivers.org › blog › how-to-limit-decimal-places-in-python
How to Limit Decimal Places in Python - CodeRivers
February 22, 2026 - f-Strings, introduced in Python 3.6, provide a more concise and intuitive way to format strings and limit decimal places. ... Similar to str.format(), ndigits is the number of decimal places, and f indicates a floating-point number.
🌐
CodeRivers
coderivers.org › blog › python-cut-off-float-2-decimal
Python: Cutting Off Floats to Two Decimal Places - CodeRivers
March 4, 2025 - This blog post will explore various ... scenarios, and best practices. Key takeaway: Use round() for general arithmetic, f-strings for display, and the decimal module for financial calculations where exact precision matters....
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python limit floats to two decimal points
Python Limit Floats to Two Decimal Points - Spark By {Examples}
May 31, 2024 - How to limit floats to two decimal points in Python? One solution to limit floats to two decimal points is to use the round() function, which rounds a