T
T
Timebird2016-02-03 14:30:37
Python
Timebird, 2016-02-03 14:30:37

Is it possible to discard k decimal places in float in Python?

Hello, next question.
Let's say there is a number 17.212123445 ...
And you need to write to memory only the first digit after the decimal point. How to do this using real and integer operations?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Michael, 2016-02-03
@Timebird

it??

>>> a=13.946
  >>> print(a)
  13.946
  >>> print("%.2f" % a)
  13.95

N
Nikita Konin, 2016-02-03
@jkjkjirf

>>> float("{0:.1f}".format(17.212123445))
17.2

A
abcd0x00, 2016-02-04
@abcd0x00

>>> n = 17.212123445
>>> n = int(n * 100) / 100
>>> n
17.21
>>>

V
Vov Vov, 2016-02-03
@balamut108

There is also a fractions module. It allows you to "correctly" perform floating point calculations. If you just want to save, then the answer is correct.
If you don't understand what I'm talking about, just run this code:'{:.30}'.format(17.33333333)

E
Evgeny Barentsev, 2019-08-20
@urkotyak

https://pythonworld.ru/osnovy/okruglenie.html
Maybe not quite right, but in my case, I was looking for exactly this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question