Related
Suppose I have 8.8333333333333339, and I want to convert it to 8.84. How can I accomplish this in Python?
round(8.8333333333333339, 2) gives 8.83 and not 8.84. I am new to Python or programming in general.
I don't want to print it as a string, and the result will be further used. For more information on the problem, please check Tim Wilson's Python Programming Tips: Loan and payment calculator.
8.833333333339 (or 8.833333333333334, the result of 106.00/12) properly rounded to two decimal places is 8.83. Mathematically it sounds like what you want is a ceiling function. The one in Python's math module is named ceil:
import math
v = 8.8333333333333339
print(math.ceil(v*100)/100) # -> 8.84
Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 102 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.
If you don't want to use the math module for some reason, you can use this (minimally tested) implementation I just wrote:
def ceiling(x):
n = int(x)
return n if n-1 < x <= n else n+1
How all this relates to the linked Loan and payment calculator problem:
From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function. This means that each month a little more than 1⁄12 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76.
It would have been equally valid to use normal rounding producing a monthly payment of 8.83 and a slightly higher final payment of 8.87. However, in the real world people generally don't like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.
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 in decimal (0.333333... ad infinitum).
If you want to ensure absolute precision, you need the decimal module:
>>> import decimal
>>> a = decimal.Decimal("8.833333333339")
>>> print(round(a,2))
8.83
You want to use the decimal module but you also need to specify the rounding mode. Here's 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')
>>>
A much simpler 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 were to print out total_price right now you would get
3.9800000000000004
But if you enclose it in a round() function like so
print(round(total_price,2))
The output equals
3.98
The round() function works by accepting two parameters. The first is the number you want to round. The second is the number of decimal places to round to.
The easiest way to do this is by using the below function, which is built in:
format()
For example:
format(1.242563,".2f")
The output would be:
1.24
Similarly:
format(9.165654,".1f")
would give:
9.2
If you round 8.8333333333339 to 2 decimals, the correct answer is 8.83, not 8.84. The reason you got 8.83000000001 is because 8.83 is a number that cannot be correctly reprecented in binary, and it gives you the closest one. If you want to print it without all the zeros, do as VGE says:
print "%.2f" % 8.833333333339 #(Replace number with the variable?)
If you want to round, 8.84 is the incorrect answer. 8.833333333333 rounded is 8.83 not 8.84. If you want to always round up, then you can use math.ceil. Do both in a combination with string formatting, because rounding a float number itself doesn't make sense.
"%.2f" % (math.ceil(x * 100) / 100)
Just for the record. You could do it this way:
def roundno(no):
return int(no//1 + ((no%1)/0.5)//1)
There, no need for includes/imports
Here is my solution for the round up/down problem
< .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
I have this code:
tax = (tax / 100) * price
and then this code:
tax = round((tax / 100) * price, 2)
round worked for me
Use the decimal module: http://docs.python.org/library/decimal.html
ََََََ
Here is a simple function to do this for you:
def precision(num,x):
return "{0:.xf}".format(round(num))
Here, num is the decimal number. x is the decimal up to where you want to round a floating number.
The advantage over other implementation is that it can fill zeros at the right end of the decimal to make a deciaml number up to x decimal places.
Example 1:
precision(10.2, 9)
will return
10.200000000 (up to 9 decimal points)
Example 2:
precision(10.2231, 2)
will return
10.22 (up to two decimal points)
This question already has answers here:
How to round a number to significant figures in Python
(26 answers)
Closed 4 years ago.
I want a variable code which rounds for example 0.91823 to 0.92 , but the number 0.00009384 should be rounded to 0.000094. I guess it's easy, but I could not find something which does the job.
For clarity I'll keep the code expanded, rather than forcing it into a one-liner.
def round2(n, numberOfDigits):
p = floor(log10(n));
# normalize
n = n * pow(10, -p);
# round
n = (n - n % pow(10, numberOfDigits)) * pow(10, p);
# return
return n;
The idea is to first 'remove' all leading zeroes by multiplying the incoming number by an appropriate power of 10.
Then use the normal rounding operator to round the new number to the appropriate radix.
And finally scale the number again.
You can print the number to 2 digits of precision, then convert back to a float by specifying the required number of decimal places:
# Format the number to scientific notation with one digit before
# the decimal point and one after, then split the sctring into the mantissa
# and exponent.
a, b = ('{0:.1E}'.format(.0000004565)).split("E")
# If the exponent is -n, get the number of required decimal digits as n+1.
c=1-int(b)
# Set up a '%0.xf' format string where x is the required number of digits,
# and use that format to print the reassembled scientific notation value
res = ('%%0.%df' % c) % float(a+"E"+b)
This works with some numbers >1, but breaks down above 99.
You could try string-manipulation:
import re
def roundToDigit(number, numDigits):
# Convert number to a string
asStr = str(number)
# Search for the first numerical digit, ignoring zeros
m = re.search("[123456789]", asStr)
if (not m):
return round(0, numDigits)
afterDecimal = m.start()
# Check if the number is in scientific notation
isExp = asStr.find("e") > -1
if (isExp):
numZeros = int(asStr[ (asStr.find("-", 1) + 1) :])
return float(round(number, numZeros + numDigits - 1))
# Check for numbers > 9
beforeDecimal = asStr.find(".")
if (beforeDecimal == -1):
return float(round(number, numDigits))
return float(round(number, afterDecimal - beforeDecimal + numDigits - 1))
Using log is probably the correct choice but if, for whatever reason, that doesn't do it for you then this will work.
This question already has answers here:
How to define a decimal class holding 1000 digits in python?
(4 answers)
Closed 7 years ago.
please go easy on me, I've been learning Python about a week!
I thought I'd try calculating Pi using the Rumanujan formula. I am confident I was able to code that correctly.
My answer is truncating and I'd like it to be represented with 200 dp. In C I'd use malloc to do this perhaps but I understand that Python doesn't work that way.
The learning point I'd like to take away from this is: Is the truncation caused by the limit of representing a float, and if so is it possible to fix?
Thanks.
import math
from decimal import *
getcontext().prec = 200
def iterate(n):
sum = 0
Decimal(sum)
sum = (math.factorial(4*n))
sum = (sum/math.pow(math.factorial(n), 4))
sum = sum*((26390*n +1103)/math.pow(396, (4*n)))
return sum
ans=0
Decimal(ans)
print "Choose the number of iterations:\n"
itnum = int(raw_input())
for n in range (0, itnum+1):
this_iteration = 0
Decimal(this_iteration)
this_iteration = iterate(n)
ans = ans + this_iteration
ans = ans*(math.pow(8, 0.5)/9801)
ans = 1/ans
print "%.200f" % ans
Your snippet
sum = 0
Decimal(sum)
leaves sum set to the int 0, and computes and throws away a Decimal equivalent. Use, instead, an assignment statement:
sum = Decimal(0)
Next, you'll need to ensure every intermediate result is also converted appropriately to Decimal (and floats by default are not).
Personally, I'd recommend using gmpy2 instead, but then, I'm biased:-).
Suppose I have 8.8333333333333339, and I want to convert it to 8.84. How can I accomplish this in Python?
round(8.8333333333333339, 2) gives 8.83 and not 8.84. I am new to Python or programming in general.
I don't want to print it as a string, and the result will be further used. For more information on the problem, please check Tim Wilson's Python Programming Tips: Loan and payment calculator.
8.833333333339 (or 8.833333333333334, the result of 106.00/12) properly rounded to two decimal places is 8.83. Mathematically it sounds like what you want is a ceiling function. The one in Python's math module is named ceil:
import math
v = 8.8333333333333339
print(math.ceil(v*100)/100) # -> 8.84
Respectively, the floor and ceiling functions generally map a real number to the largest previous or smallest following integer which has zero decimal places — so to use them for 2 decimal places the number is first multiplied by 102 (or 100) to shift the decimal point and is then divided by it afterwards to compensate.
If you don't want to use the math module for some reason, you can use this (minimally tested) implementation I just wrote:
def ceiling(x):
n = int(x)
return n if n-1 < x <= n else n+1
How all this relates to the linked Loan and payment calculator problem:
From the sample output it appears that they rounded up the monthly payment, which is what many call the effect of the ceiling function. This means that each month a little more than 1⁄12 of the total amount is being paid. That made the final payment a little smaller than usual — leaving a remaining unpaid balance of only 8.76.
It would have been equally valid to use normal rounding producing a monthly payment of 8.83 and a slightly higher final payment of 8.87. However, in the real world people generally don't like to have their payments go up, so rounding up each payment is the common practice — it also returns the money to the lender more quickly.
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 in decimal (0.333333... ad infinitum).
If you want to ensure absolute precision, you need the decimal module:
>>> import decimal
>>> a = decimal.Decimal("8.833333333339")
>>> print(round(a,2))
8.83
You want to use the decimal module but you also need to specify the rounding mode. Here's 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')
>>>
A much simpler 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 were to print out total_price right now you would get
3.9800000000000004
But if you enclose it in a round() function like so
print(round(total_price,2))
The output equals
3.98
The round() function works by accepting two parameters. The first is the number you want to round. The second is the number of decimal places to round to.
The easiest way to do this is by using the below function, which is built in:
format()
For example:
format(1.242563,".2f")
The output would be:
1.24
Similarly:
format(9.165654,".1f")
would give:
9.2
If you round 8.8333333333339 to 2 decimals, the correct answer is 8.83, not 8.84. The reason you got 8.83000000001 is because 8.83 is a number that cannot be correctly reprecented in binary, and it gives you the closest one. If you want to print it without all the zeros, do as VGE says:
print "%.2f" % 8.833333333339 #(Replace number with the variable?)
If you want to round, 8.84 is the incorrect answer. 8.833333333333 rounded is 8.83 not 8.84. If you want to always round up, then you can use math.ceil. Do both in a combination with string formatting, because rounding a float number itself doesn't make sense.
"%.2f" % (math.ceil(x * 100) / 100)
Just for the record. You could do it this way:
def roundno(no):
return int(no//1 + ((no%1)/0.5)//1)
There, no need for includes/imports
Here is my solution for the round up/down problem
< .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
I have this code:
tax = (tax / 100) * price
and then this code:
tax = round((tax / 100) * price, 2)
round worked for me
Use the decimal module: http://docs.python.org/library/decimal.html
ََََََ
Here is a simple function to do this for you:
def precision(num,x):
return "{0:.xf}".format(round(num))
Here, num is the decimal number. x is the decimal up to where you want to round a floating number.
The advantage over other implementation is that it can fill zeros at the right end of the decimal to make a deciaml number up to x decimal places.
Example 1:
precision(10.2, 9)
will return
10.200000000 (up to 9 decimal points)
Example 2:
precision(10.2231, 2)
will return
10.22 (up to two decimal points)
This question already has answers here:
Is floating point arbitrary precision available?
(5 answers)
Closed 2 years ago.
I want to represent a floating-point number as a string rounded to some number of significant digits, and never using the exponential format. Essentially, I want to display any floating-point number and make sure it “looks nice”.
There are several parts to this problem:
I need to be able to specify the
number of significant digits.
The number of significant digits
needs to be variable, which can't be
done with with the string formatting
operator. [edit] I've been corrected; the string formatting operator can do this.
I need it to be rounded the way a
person would expect, not something
like 1.999999999999
I've figured out one way of doing this, though it looks like a work-round and it's not quite perfect. (The maximum precision is 15 significant digits.)
>>> def f(number, sigfig):
return ("%.15f" % (round(number, int(-1 * floor(log10(number)) + (sigfig - 1))))).rstrip("0").rstrip(".")
>>> print f(0.1, 1)
0.1
>>> print f(0.0000000000368568, 2)
0.000000000037
>>> print f(756867, 3)
757000
Is there a better way to do this? Why doesn't Python have a built-in function for this?
It appears there is no built-in string formatting trick which allows you to (1) print floats whose first significant digit appears after the 15th decimal place and (2) not in scientific notation. So that leaves manual string manipulation.
Below I use the decimal module to extract the decimal digits from the float.
The float_to_decimal function is used to convert the float to a Decimal object. The obvious way decimal.Decimal(str(f)) is wrong because str(f) can lose significant digits.
float_to_decimal was lifted from the decimal module's documentation.
Once the decimal digits are obtained as a tuple of ints, the code below does the obvious thing: chop off the desired number of sigificant digits, round up if necessary, join the digits together into a string, tack on a sign, place a decimal point and zeros to the left or right as appropriate.
At the bottom you'll find a few cases I used to test the f function.
import decimal
def float_to_decimal(f):
# http://docs.python.org/library/decimal.html#decimal-faq
"Convert a floating point number to a Decimal with no loss of information"
n, d = f.as_integer_ratio()
numerator, denominator = decimal.Decimal(n), decimal.Decimal(d)
ctx = decimal.Context(prec=60)
result = ctx.divide(numerator, denominator)
while ctx.flags[decimal.Inexact]:
ctx.flags[decimal.Inexact] = False
ctx.prec *= 2
result = ctx.divide(numerator, denominator)
return result
def f(number, sigfig):
# http://stackoverflow.com/questions/2663612/nicely-representing-a-floating-point-number-in-python/2663623#2663623
assert(sigfig>0)
try:
d=decimal.Decimal(number)
except TypeError:
d=float_to_decimal(float(number))
sign,digits,exponent=d.as_tuple()
if len(digits) < sigfig:
digits = list(digits)
digits.extend([0] * (sigfig - len(digits)))
shift=d.adjusted()
result=int(''.join(map(str,digits[:sigfig])))
# Round the result
if len(digits)>sigfig and digits[sigfig]>=5: result+=1
result=list(str(result))
# Rounding can change the length of result
# If so, adjust shift
shift+=len(result)-sigfig
# reset len of result to sigfig
result=result[:sigfig]
if shift >= sigfig-1:
# Tack more zeros on the end
result+=['0']*(shift-sigfig+1)
elif 0<=shift:
# Place the decimal point in between digits
result.insert(shift+1,'.')
else:
# Tack zeros on the front
assert(shift<0)
result=['0.']+['0']*(-shift-1)+result
if sign:
result.insert(0,'-')
return ''.join(result)
if __name__=='__main__':
tests=[
(0.1, 1, '0.1'),
(0.0000000000368568, 2,'0.000000000037'),
(0.00000000000000000000368568, 2,'0.0000000000000000000037'),
(756867, 3, '757000'),
(-756867, 3, '-757000'),
(-756867, 1, '-800000'),
(0.0999999999999,1,'0.1'),
(0.00999999999999,1,'0.01'),
(0.00999999999999,2,'0.010'),
(0.0099,2,'0.0099'),
(1.999999999999,1,'2'),
(1.999999999999,2,'2.0'),
(34500000000000000000000, 17, '34500000000000000000000'),
('34500000000000000000000', 17, '34500000000000000000000'),
(756867, 7, '756867.0'),
]
for number,sigfig,answer in tests:
try:
result=f(number,sigfig)
assert(result==answer)
print(result)
except AssertionError:
print('Error',number,sigfig,result,answer)
If you want floating point precision you need to use the decimal module, which is part of the Python Standard Library:
>>> import decimal
>>> d = decimal.Decimal('0.0000000000368568')
>>> print '%.15f' % d
0.000000000036857
Here is a snippet that formats a value according to the given error bars.
from math import floor, log10, round
def sigfig3(v, errplus, errmin):
i = int(floor(-log10(max(errplus,errmin)) + 2))
if i > 0:
fmt = "%%.%df" % (i)
return "{%s}^{%s}_{%s}" % (fmt % v,fmt % errplus, fmt % errmin)
else:
return "{%d}^{%d}_{%d}" % (round(v, i),round(errplus, i), numpy.round(i))
Examples:
5268685 (+1463262,-2401422) becomes 5300000 (+1500000,-2400000)
0.84312 +- 0.173124 becomes 0.84 +- 0.17
Arbitrary precision floats are needed to properly answer this question. Therefore using the decimal module is a must. There is no method to convert a decimal to a string without ever using the exponential format (part of the original question), so I wrote a function to do just that:
def removeExponent(decimal):
digits = [str(n) for n in decimal.as_tuple().digits]
length = len(digits)
exponent = decimal.as_tuple().exponent
if length <= -1 * exponent:
zeros = -1 * exponent - length
digits[0:0] = ["0."] + ["0"] * zeros
elif 0 < -1 * exponent < length:
digits.insert(exponent, ".")
elif 0 <= exponent:
digits.extend(["0"] * exponent)
sign = []
if decimal.as_tuple().sign == 1:
sign = ["-"]
print "".join(sign + digits)
The problem is trying to round to significant figures. Decimal's "quantize()" method won't round higher than the decimal point, and the "round()" function always returns a float. I don't know if these are bugs, but it means that the only way to round infinite precision floating point numbers is to parse it as a list or string and do the rounding manually. In other words, there is no sane answer to this question.