0

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?

creator64
  • 5
  • 3

1 Answers1

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

GAP2002
  • 870
  • 4
  • 20