How can I stop printing a float 3 spaces after decimal?

Kyle Me

I'm sorry, I know this must be a duplicate, I can't find where else it's posted. Please feel free to link me to the original question and mark this as duplicate.

I would like to print a 3 digits of a number AFTER the decimal point in it. For example:

number = 523.637382

I would like to print: 523.637 I have a feeling I can use something similar to this

print(str(number)[:7])
>>>523.637

However, this will not work if the number before the decimal is not 3 decimals.

Bonus points: Would this be easy?

number = 500.220
#magic
>>>500.22

number = 500.2000003
#magic
>>>500.2
Ewharris

A (built-in) function that could do this is round:

>>> number = 523.637382
>>> rounded = round(number, 3) # 3 decimal places, for example
>>> rounded
523.637

This has already been answered for example here.

The good news, to answer the second part of your question, is that the round function automatically removes trailing zeroes. It's much harder to retain the zeros if you're defining a new variable: you need the decimal module; but it looks that that isn't necessary here.

>>> number = 523.60000001
>>> rounded = round(number, 3)
>>> rounded
523.6

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How i can stop the code after opening a dialog box?

분류에서Dev

How can I stop the timer?

분류에서Dev

How can I stop Gmail from keeping me logged in after I close the browser?

분류에서Dev

How can I show the decimal and two places after it when converting an int to double?

분류에서Dev

How can I stop windows re-positioning after waking from sleep?

분류에서Dev

How can I stop this looping statement

분류에서Dev

How can I remove all extraneous spaces from a *.docx file?

분류에서Dev

How can I remove the extra spaces between my output?

분류에서Dev

How can I remove the decimal parts from numbers in Kotlin?

분류에서Dev

How can I remove last digit from decimal number in PHP

분류에서Dev

How do i stop a while loop after a certain time in C

분류에서Dev

How can I stop gvim from crashing when opening a file?

분류에서Dev

How can I stop being prompted to unlock the 'default' keyring on boot?

분류에서Dev

How can I stop terminal and firefox from launching on startup?

분류에서Dev

How can I stop windows from always booting first?

분류에서Dev

How can I stop accumulated Google Chrome background processes?

분류에서Dev

How can I stop pages from scrolling with an empty hyperlink jump?

분류에서Dev

How can I stop excel from rounding to .00?

분류에서Dev

How can I stop event to trigger in other magento module

분류에서Dev

How can I stop my controller from moving my mouse?

분류에서Dev

How can I get full-ranged random float values?

분류에서Dev

How do I stop a windows from closing after I raise wx.EVT_CLOSE in wxPython?

분류에서Dev

How does this website know I have copied data (and how can I stop it?)

분류에서Dev

How to stop my GUI from printing text indefinitely? - Java

분류에서Dev

How can I suppress printing the full path to subdirectories when using CDPATH=".:/some/path"?

분류에서Dev

How does `*((*arr+(i*3))+j)` work when printing a 2D array `arr`?

분류에서Dev

RAILS 3: How can I reload a partial?

분류에서Dev

How can I set 3 values in spinner

분류에서Dev

How can I stop Document Viewer from always opening full-screen?

Related 관련 기사

  1. 1

    How i can stop the code after opening a dialog box?

  2. 2

    How can I stop the timer?

  3. 3

    How can I stop Gmail from keeping me logged in after I close the browser?

  4. 4

    How can I show the decimal and two places after it when converting an int to double?

  5. 5

    How can I stop windows re-positioning after waking from sleep?

  6. 6

    How can I stop this looping statement

  7. 7

    How can I remove all extraneous spaces from a *.docx file?

  8. 8

    How can I remove the extra spaces between my output?

  9. 9

    How can I remove the decimal parts from numbers in Kotlin?

  10. 10

    How can I remove last digit from decimal number in PHP

  11. 11

    How do i stop a while loop after a certain time in C

  12. 12

    How can I stop gvim from crashing when opening a file?

  13. 13

    How can I stop being prompted to unlock the 'default' keyring on boot?

  14. 14

    How can I stop terminal and firefox from launching on startup?

  15. 15

    How can I stop windows from always booting first?

  16. 16

    How can I stop accumulated Google Chrome background processes?

  17. 17

    How can I stop pages from scrolling with an empty hyperlink jump?

  18. 18

    How can I stop excel from rounding to .00?

  19. 19

    How can I stop event to trigger in other magento module

  20. 20

    How can I stop my controller from moving my mouse?

  21. 21

    How can I get full-ranged random float values?

  22. 22

    How do I stop a windows from closing after I raise wx.EVT_CLOSE in wxPython?

  23. 23

    How does this website know I have copied data (and how can I stop it?)

  24. 24

    How to stop my GUI from printing text indefinitely? - Java

  25. 25

    How can I suppress printing the full path to subdirectories when using CDPATH=".:/some/path"?

  26. 26

    How does `*((*arr+(i*3))+j)` work when printing a 2D array `arr`?

  27. 27

    RAILS 3: How can I reload a partial?

  28. 28

    How can I set 3 values in spinner

  29. 29

    How can I stop Document Viewer from always opening full-screen?

뜨겁다태그

보관