How to round a floating point number to a specific decimal place?

Suppose I have 8.8333333333333339 and I want to convert it to 8.84 . How can I do this in Python?

round(8.8333333333333339, 2) gives 8.83 and not 8.84 . I am new to Python or programming in general.

I do not want to print this as a string, and the result will be used in the future. For more information about the problem, please check out Tim Wilson Python Programming Tips: Credit and Payment Calculator .

+76
python floating-point
Dec 23 '10 at 12:13
source share
12 answers

8.833333333339 (or 8.833333333333334 , result 106.00/12 ), correctly rounded to two decimal places, is 8.83 . Mathematically, it looks like you want a ceiling function . The one in the Python math module is called ceil :

 import math v = 8.8333333333333339 print(math.ceil(v*100)/100) # -> 8.84 

Accordingly, the floor and ceiling functions usually map a real number to the largest previous or smallest next integer that has zero decimal places - therefore, to use them for two decimal places, the number is first multiplied by 10 2 (or 100) to shift decimal point and then divide by it to compensate.

If for some reason you do not want to use the math module, you can use this (minimally tested) implementation that I just wrote:

 def ceiling(x): n = int(x) return n if n-1 < x <= n else n+1 

How does this relate to the related issue of a credit and billing calculator

loan calculator output screenshot

An example of the conclusion shows that they rounded the monthly payment, which many call the effect of the ceiling function. This means that a little more than 1 & frasl; 12 of the total amount. This made the final payment a little less than usual - leaving the remaining unpaid balance of only 8.76 .

It would be fair to use regular rounding, creating a monthly payment of 8.83 and a slightly higher final payment of 8.87 . However, in the real world, people usually don’t like when their payments grow, so rounding off each payment is a common practice - it also returns money to the lender faster.

+96
Dec 23 '10 at 13:13
source share

This is normal (and has nothing to do with Python), because 8.83 cannot be represented exactly as a binary float, just as 1/3 cannot be represented exactly as decimal (0.333333 ... ad infinitum).

If you want to ensure absolute accuracy, you will need the decimal module:

 >>> import decimal >>> a = decimal.Decimal("8.833333333339") >>> print(round(a,2)) 8.83 
+63
Dec 23 '10 at 12:16
source share

You want to use the decimal module, but you also need to specify a rounding mode. Here is an example:

 >>> import decimal >>> decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_UP) Decimal('8.34') >>> decimal.Decimal('8.333333').quantize(decimal.Decimal('.01'), rounding=decimal.ROUND_DOWN) Decimal('8.33') >>> 
+22
Dec 23 '10 at 15:48
source share

An easier way is to simply use the round () function. Here is an example.

 total_price = float() price_1 = 2.99 price_2 = 0.99 total_price = price_1 + price_2 

If you want to print total_price right now, you will get

 3.9800000000000004 

But if you wrap it in the round () function, for example,

 print(round(total_price,2)) 

The output is

 3.98 

The round () function works by accepting two parameters. The first is the number you want to round. Secondly, the number of decimal places is rounded to.

+11
Feb 24 '15 at
source share

If you round 8.8333333333339 to 2 decimal places, the correct answer would be 8.83, not 8.84. The reason you got 8.83000000001 is because 8.83 is a number that cannot be correctly reproduced in binary format, and it gives you the closest. If you want to print it without all zeros, do as VGE says:

 print "%.2f" % 8.833333333339 #(Replace number with the variable?) 
+7
Dec 23 '10 at 12:31
source share

If you want to round, 8.84 is the wrong answer. 8.833333333333 rounded 8.83 not 8.84. If you want to always round, you can use math.ceil. Do both in combination with string formatting, because rounding a floating point number does not make sense.

 "%.2f" % (math.ceil(x * 100) / 100) 
+5
Dec 23 '10 at 1:09 p.m.
source share

For recording only. You can do it as follows:

 def roundno(no): return int(no//1 + ((no%1)/0.5)//1) 

There is no need to include / import

+3
Jan 22 '15 at 4:34
source share

The easiest way to do this is to use the built-in function:

 format() 

For example:

 format(1.242563,".2f") 

The output will be:

 1.24 

Same:

 format(9.165654,".1f") 

will give:

 9.2 
+3
Feb 07 '18 at 23:48
source share
+1
Dec 23 '10 at 12:15
source share

Here is my solution to the problem of rounding up / down

 < .5 round down > = .5 round up 



 import math def _should_round_down(val: float): if val < 0: return ((val * -1) % 1) < 0.5 return (val % 1) < 0.5 def _round(val: float, ndigits=0): if ndigits > 0: val *= 10 ** (ndigits - 1) is_positive = val > 0 tmp_val = val if not is_positive: tmp_val *= -1 rounded_value = math.floor(tmp_val) if _should_round_down(val) else math.ceil(tmp_val) if not is_positive: rounded_value *= -1 if ndigits > 0: rounded_value /= 10 ** (ndigits - 1) return rounded_value # test # nr = 12.2548 # for digit in range(0, 4): # print("{} decimals : {} -> {}".format(digit, nr, _round(nr, digit))) # output # 0 decimals : 12.2548 -> 12 # 1 decimals : 12.2548 -> 12.0 # 2 decimals : 12.2548 -> 12.3 # 3 decimals : 12.2548 -> 12.25 
+1
Nov 11 '16 at 15:55
source share

Here is a simple function to do this for you:

 def precision(num,x): return "{0:.xf}".format(round(num)) 

Here num is a decimal number. x is the decimal number to where you want to round the floating number.

The advantage over other implementations is that it can fill in the zeros at the right end of the decimal to make the number of the decimal number up to x decimal places.

Example 1:

 precision(10.2, 9) 

will be back

10.200000000 (up to 9 decimal places)

Example 2:

 precision(10.2231, 2) 

will be back

10.22 (up to two decimal places)

0
Mar 14 '18 at 2:46
source share

I have this code:

 tax = (tax / 100) * price 

and then this code:

 tax = round((tax / 100) * price, 2) 

the round worked for me

0
Aug 15 '18 at 16:05
source share



All Articles