# python – Rounding floats to nearest 10th

## python – Rounding floats to nearest 10th

Heres how to round a number to the nearest tenth:

```
>>> round(21.3331, 1)
21.3
```

Heres how to print out a floating point number with one decimal point:

```
>>> print %.1f % (21.3331,)
21.3
```

Note that if you do it using `%d`

it will get printed as rounded to the nearest integer:

```
>>> print %d % (21.3331,)
21
```

See the String Formatting Operations docs for more on how the `%`

formatting works.

Heres as simple one.

`roundf(1.345 * 100) / 100`

You simply multiply it by 100 before dividing it by 100 which preserves the value to 2 decimal places. This should be much more efficient than transforming it to a string and back.

#### python – Rounding floats to nearest 10th

Youre converting to `int`

. That means your result is an `int`

. `int`

s dont have fractional parts. Also, `%d`

means format as `int`

, so it will implicitly convert to `int`

before printing. Use `%f`

in your format string.

```
def add(op1,op2):
result = round(op1 + op2, 1)
print()
print (Output: %0.1f + %0.1f = %0.1f % (op1, op2, result))
```