How to round a value up WITH ndigits [duplicate] - python

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).

Related

How to round float in multiple of 5 [duplicate]

This question already has answers here:
Round to 5 (or other number) in Python
(21 answers)
How do I round to the nearest 0.5?
(10 answers)
Closed 2 years ago.
I got this number 1.12412 and I want to round it to 1.12415 or 1.12410 (muliple of 5 in last decimal)
If using the Round(X,4) function I get 1.1241 (4 decimals).
Is there a function that can make that happen?
Thanks!
There is an answer in stack but using c# not python
My way to do that is to specify rounding unit first and then simple trick as below:
import numpy as np
rounding_unit = 0.00005
np.round(1.12412/rounding_unit) * rounding_unit
You may:
Multiply your number by 2
Use Round(X,4)
Divide the result by 2
profit!!!

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

When Rounding to nearest hundreds, how do I include 0s [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here

How python calculate this division? [duplicate]

This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10

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