How do I round a number in Python (Thonny)? [duplicate] - python

This question already has answers here:
round() doesn't seem to be rounding properly
(20 answers)
Closed 6 years ago.
Consider:
def välgu_kaugus(aeg):
kiirus = 300 / 1000
valem = aeg * kiirus
return valem
print(välgu_kaugus(float(input("Mitu sekundid kulus välgu nägemiseks müristamise kuulmiseni? "))))
This is my little shitty program. When I input 15 it gives me 4.5, but I want it to round 4.5 to 5, but using the round command it rounds my 4.5 to 4 for some reason. How can I make it to round to 5?

Use round(). For example:
>>> round(4.5) # Your number, rounded to ceil value
5.0
>>> round(4.3) # rounded to floor value
4.0
>>> round(4.7) # rounded to ceil value
5.0

Solution to your problem is to use ceiling.
import math
print math.ceil(4.5) // 5
Here are some references on how to round a number in python:
https://www.tutorialspoint.com/python/number_round.htm
How do you round UP a number in Python?

Related

Round to nearest number between two numbers [duplicate]

This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
Closed last year.
How do I round a fraction to the nearest 0.5 between 0.0 and 5.0?
Let's say I've got 11/150, it should convert to 0.0. In addition, 75/150 should convert to 2.5
As for the code I've got this but I'm lost as to where to go from here
# number is 11, and max is 150
number = float(percent / max)
number = round(p/0.5)*0.5
However, it needs to round between 0.0 and 5.0 rather than 0 and 1.
EDIT 1:
It's best to treat it as a percentage. For example, let's say the percent given is 5o% (50/100, for ease). This means the result should be 2.5
In the same respect, 51% would still be 2.5.
However, 60% (60/100) would be 3.0
Multiply by 2, round, then divide by 2
if you want nearest quarter, multiply by 4, divide by 4, etc

How to round in python? example, 45.722 to 45.72, 45.0 to 45.00 [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 2 years ago.
I wanna round after decimal point. I know that the function round does it but when the number has only zeros after decimal point I also wanna keep the amount of zeros that I want, becouse python leaves only 1 zero.
round(54.983839,2) == 54.98
round(54.0, 2) == 54.0 # and here I want 54.00
Use string formatting in python: "%.2f" % (54.0).
You can use format():
print(format(54.0, ".2f"))
The output will be:
54.00

Round 3.666666 to 3.66 in python [duplicate]

This question already has answers here:
round down to 2 decimal in python
(8 answers)
Closed 3 years ago.
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)
How about using
import math
x = math.floor(x * 100) / 100
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

Why round(4.5) == 4 and round(5.5) == 6 in Python 3.5? [duplicate]

This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Closed 5 years ago.
Looks like both 4.5 and 5.5 have exact float representations in Python 3.5:
>>> from decimal import Decimal
>>> Decimal(4.5)
Decimal('4.5')
>>> Decimal(5.5)
Decimal('5.5')
If this is the case, then why
>>> round(4.5)
4
>>> round(5.5)
6
?
In Python 3, exact half way numbers are rounded to the nearest even result. This behavior changed in Python 3
The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.round([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.
Python 3 uses Bankers Rounding, which rounds .5 values to the closest even number.

Python rounding float number numbers issue [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
I have the following piece of code which I run with python 2.7.9:
import math
print 0.6/0.05
print math.floor(0.6/0.05)
print int(0.6/0.05)
The output is:
12.0
11.0
11
Why is it turning my 12.0 to an 11.0 when I use floor or int? round() will work but does not suit my use case.
I have this running with 0.55,0.60,0.65,0.70... and everything works fine except for 0.6.
Any idea?
If you know the required resolution in advance, you can multiply your input numbers accordingly and then make the required calculations safely.
Let's say that the resolution is 0.01. Then:
# original inputs
n = 0.6
d = 0.05
# intermediate values
n1 = int(n*100)
d1 = int(d*100)
# integer calculation
r = n1 // d1
# or
# floating point calculation
r = float(n1) / float(d1)

Categories