-2

I want to convert 1.368090543211 into 1.36 or 1.3. please tell me how can I do it. thank you. I tried this, and the answer is also what I want. But I want a way to shorten a random decimal value.

KM_TO_MILES

miles = 5.64
def KM_TO_MILES(miles):
    KM = ("{:.2f}" .format(miles * 1.6))
    print(KM , "KM")
  • I hope to get a simpler and an easy way. – Moaz Haroon Aug 14 '20 at 16:33
  • This works just fine? As does the below answer – TheLazyScripter Aug 14 '20 at 16:35
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) (you get a badge!) and read through the [help center](https://stackoverflow.com/help), in particular [good question?](https://stackoverflow.com/help/how-to-ask) Your best bet here is to do your research, [search](https://stackoverflow.com/help/searching) for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research, post a [Verifiable Example](http://stackoverflow.com/help/mcve) of your attempt and specifically say where you're stuck. People will be glad to help. – Jacob Aug 14 '20 at 17:37
  • Does this answer your question? [How to round to 2 decimals with Python?](https://stackoverflow.com/questions/20457038/how-to-round-to-2-decimals-with-python) – tgdavies Aug 16 '20 at 06:34

1 Answers1

1

Use the built-in function round to round a number.

For example, to round a number to 2 decimals:

round(1.368090543211, 2)

Result:

1.37
zvone
  • 18,045
  • 3
  • 49
  • 77