Round 3.666666 to 3.66 in python [duplicate] - python

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

Related

How the Ceil function is calculating the value if this expression? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
How to round a floating point number up to a certain decimal place?
(12 answers)
Closed 7 months ago.
ceil((14.84 - 14.04)/0.08)
output - 11
I was expecting the output to be 10 when manually calculated but when running it in python, it is giving output as 11
Is floating point math broken?
The float value from your equation is actually 10.000000000000009 because of how floats are handled (see link for more information). So, even though it is such a small amount above 10 the ceiling function will still place it at 11.
You can try rounding the number to a decimal point that you trust to get the value you want:
from math import ceil
ceil(round((14.84 - 14.04)/0.08, 2))
Output: 10

How to round a value up WITH ndigits [duplicate]

This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 2 years ago.
The built-in function round() will round a value down but I want to know how to round a value up.
i know that this is possible with math.ceil() but the thing is that round() has the keyword argument "ndigits" and math.ceil() doesn't. so for example:
>>> round(1024, ndigits=-3)
1000
but i want 1100.
Is there a solution for this?
import math
def round(number, n):
return math.ceil(number * math.pow(10, n+1))*(math.pow(10,-(n+1)))
print(round(1024, -3))
# 1100.0
A simple function like this would suffice, multiply by 10^(n+1), find the ceiling of that number, then multiply by 10^-n-1 (equivalent to dividing by 10^n+1).

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?

Remove decimal places to certain digits without rounding [duplicate]

This question already has answers here:
How to truncate float values?
(31 answers)
Closed 9 years ago.
I've found dozen of answers, but non of them is what I'm looking for, I don't want to round up or down, I know that I can round numbers as follow:
>>> print('%.3f' % 15.555555)
15.556
>>> round(15.555555, 3)
15.666
But I need to get 15.555. Should I use regex?
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3

Categories