I want a to be rounded to 13.95.

```>>> a
13.949999999999999
>>> round(a, 2)
13.949999999999999
```

The round function does not work the way I expected.

Dec 2, 2020 in Python 187 views

## 1 answer to this question.

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 a decimal.
• 65,850 points

## How to round a floating point number up to certain decimal place in Python?

This is normal (and has nothing to do ...READ MORE

## How to calculate time interval between two time strings in Python

Try doing this - It is efficient for ...READ MORE

## How to calculate cosine similarity between two lists using Pythong?

You should try SciPy. It has a bunch ...READ MORE

## How do I access a user by his/her ID using django.contrib.auth.models.User in the latest release of Django?

Hi, there is only that way and ...READ MORE

## How can I update Git Code into a Docker container directly?

Hi all, with regard to the above ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE