1

How can I render decimal numbers in Python? For example, in the number 3.14373873 only up to and including the number 4.

I want to round the decimal numbers from a specific place:

As example:

3.14373873 -> 3.14
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Tan
  • 13
  • 2

4 Answers4

2

you can use the round() function:

https://www.w3schools.com/python/ref_func_round.asp

Example:

p = round(50.12345,3)
print(p)

Output:

 50.123

Example 2:

# for integers
print(round(15))

# for floating point
print(round(51.6))
print(round(51.5))
print(round(51.4))

Output:

15  
52  
52  
51  
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
1

You can do this:

print(round(3.14373873, 4))   
kleinohad
  • 5,800
  • 2
  • 26
  • 34
1

you can use the round() function:-

Example :

x = round(1.54321, 2)

print(x)

  • You can mention the value after the how many digits you have to store on your program

  • Your output will be 1.54 because of the mention of 2 digits...

  • If you have mentioned 3 digits then the output will be 1.543 like that

0

The round function from the standard python library can be used to round floats to the desired number of digits after decimal point -:

round(decimal_num, num_of_digits_to_round_to)

To round 3.14373873 to just 3.14, you can do the following -:

round(3.14373873, 2)

Refer to official python wiki for more info -:

round(number, ndigits=None)

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

typedecker
  • 1,351
  • 2
  • 13
  • 25