I am getting output 15.36 but I want only 15. I am using round function e.g round(15.36,2) but still getting the same result.
The documentation explains the arguments for the round function pretty well:
round(number[, ndigits])
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.
So if you want to round to the nearest integer just use it without the second argument:
>>> round(15.36)
15
Use the round function in python
round(number, ndigits)
So, for your example use round(15.36,0)
Related
I don't understand how format & rounding numbers works, because for instance:
"{:.0f}".format(234.50) # returns 234
"{:.0f}".format(235.50) # returns 236
"{:.0f}".format(236.50) # returns 236
"{:.0f}".format(237.50) # returns 238
And so on...
Am I missing something?
Thanks for your help!
Python rounds to nearest integer, but if the decimal is 0.5 it rounds to nearest even integer. This method is called round half to even and is common in many programming languages.
This is confirmed in the documentation of the round function as well.
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.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if called with one argument, otherwise of the same type as number.
For a general Python object number, round(number, ndigits) delegates to number.__round__(ndigits).
I'm trying to print a very small p-value to a plot (p-value = 4.3289045262877305e-11). I need to round it to have fewer digits after the decimal so it will fit (4.33e-11) but when I'm using round(,3) it returns 0.0. How can I return the number like this 4.33e-11?
When you use round you are changing the value and in your example rounding to the nearest thousandth. You only want to change the display format.
"{:.2e}".format(4.3289045262877305e-11)
will create a string with the representation you want. 4.33e-11
More details: https://kite.com/python/answers/how-to-print-a-number-in-scientific-notation-in-python
When using round(,3) you are rounding up the actual value to 0,00, not the displayed value.
If you actually need to round the value itself and not the displayed one, I would suggest you to multiply it by e11, then round (,3), then devide it by e11 to get the rounded number.
I don't understand how format & rounding numbers works, because for instance:
"{:.0f}".format(234.50) # returns 234
"{:.0f}".format(235.50) # returns 236
"{:.0f}".format(236.50) # returns 236
"{:.0f}".format(237.50) # returns 238
And so on...
Am I missing something?
Thanks for your help!
Python rounds to nearest integer, but if the decimal is 0.5 it rounds to nearest even integer. This method is called round half to even and is common in many programming languages.
This is confirmed in the documentation of the round function as well.
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.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if called with one argument, otherwise of the same type as number.
For a general Python object number, round(number, ndigits) delegates to number.__round__(ndigits).
I am trying to write a function to round a floating point number up to n decimal places. The function can take one or two arguments. If there is only one argument the number should be rounded to two decimal places.
This is where I have gotten so far:
def roundno(num,point=2):
import math
x=1*(math.pow(10,-point))
round=0
while (num>x):
while(num>0):
round+=num/10
num=num/10
round*=10
round+=num/10
num=num/10
round*=0.1
return round
I am getting infinity as the output, every time... Where did I go wrong?
I can't see how your algorithm is supposed to round numbers. I guess a similar strategy could work, but you'd need a subtraction in there somewhere...
One way to do this would be to convert the argument to a string, adjust the number of digits after the decimal point, and then convert the string back to a float, but I suspect that your teacher would not like that solution. :)
Here's a simple way to do rounding arithmetically:
def roundno(num, point=2):
scale = 10.0 ** point
return int(num * scale) / scale
data = [123, 12.34, 1.234, 9.8765, 98.76543]
for n in data:
print n, roundno(n), roundno(n, 3)
output
123 123.0 123.0
12.34 12.34 12.34
1.234 1.23 1.234
9.8765 9.87 9.876
98.76543 98.76 98.765
This simply drops unwanted digits, but it's not hard to modify it to round up or off (your question isn't clear on exactly what type of rounding you want).
Note that this function doesn't check the point argument. It really should check that it's a non-negative integer and raise ValueError with an appropriate error message otherwise.
I am working on a project where it has to take user inputs and do calculations.
What I am aiming for is the print to appear as
Inform the customer they saved 0.71 today
Not
Inform the customer they saved 0.7105000000000001 today
Is there something I can put into the same line of code with the print function to have it be rounded? Or do I have to modify each variable.
I can post my code if requested.
You can use the builtin round() function and float formatting:
>>> print "{0:0.2f}".format(round(x, 2))
0.71
Some Notes:
{0.2f} will format a float to 2 decimal places.
round(x, 2) will round up to 2 decimal places.
Side Note: round() is really necessary IHMO if you want to "round" the number before "display". It really depends on what you're doing!
round() is return the floating point value number rounded to ndigits digits after the decimal point. which takes as first argument the number and the second argument is the precision
no = 0.7105000000000001
print round(no, 2)
second solution:
print "%.2f" % 0.7105000000000001
use decimal instead of round()
from decimal import *
print(round(8.494,2)) # 8.49
print(round(8.495,2)) # 8.49
print(round(8.496,2)) # 8.5
print(Decimal('8.494').quantize(Decimal('.01'), rounding=ROUND_HALF_UP)) #8.49
print(Decimal('8.495').quantize(Decimal('.01'), rounding=ROUND_HALF_UP)) #8.50
print(Decimal('8.496').quantize(Decimal('.01'), rounding=ROUND_HALF_UP)) #8.50