Round to nearest number between two numbers [duplicate] - python

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

Related

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

Add two Float values and get up to 2 precision values [duplicate]

This question already has answers here:
How to print float to n decimal places including trailing 0s?
(5 answers)
Closed 5 years ago.
Can we get up to 2 precision values when we add 2 float values of containing 0 as 2nd precision without changing its type as Float
I have a snippet like
a = 1.20+1.20
print a
the output should be like
2.40
but I got the output as 2.4 because python rounds off
I've tried like this ,
from decimal import *
getcontext().prec = 3
Decimal(1.20)+Decimal(1.20)
Decimal('2.40')
But every time I need to change the precision value!
can we get as 2.40 without changing its type?
It should contain a float value as 2.40, not a string!
Yes! you can round a float number.
>>> round(2.675, 2)
2.67
You can see more details in python's documentation

Python 3 code - Rounding integers [duplicate]

This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 6 years ago.
I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67-->70 and 64-->70. Here is my code for rounding so far:
##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)
I would appreciate it if you could answer simple and explain how it works.
One way of rounding up would be to use integer division to go down to the precision you want and then multiplying back up. e.g.,:
Sumrounded = SumOfusrinput // (-10) * (-10)

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

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?

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