0

I tired to round 3.666666 for two digit and get 3.66. But Round() function give me 3.67. Is there a way to solve this problem with the round function without converting it to string type?

a=round(3.666666,2)

  • 3.666666 rounded to two digits is 3.67 (not 3.66). It would be easier to convert to string and back for this non-standard rounding. – Tammo Heeren Sep 03 '19 at 05:08
  • 1
    @TammoHeeren There are different types of rounding: Round to the nearest, round up, and round down. It is in no way _non-standard_. – Selcuk Sep 03 '19 at 05:09

2 Answers2

1

How about using

import math
x = math.floor(x * 100) / 100
Tammo Heeren
  • 1,966
  • 3
  • 15
  • 20
0

Use math.floor() instead of round. floor rounds down, ceil rounds up, round rounds mathematically, either up or down.

To round to two characters after the decimal point, you can multiply it with 10^2 (100) before rounding and then divide it afterwards by the same number.

Here is an example:

import math
math.floor(value * 100) / 100
ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87