3

I need to round up a floating-point number. For example 4.00011 . The inbuilt function round() always rounds up when the number is > .5 and rounds down when <= 5. This is very good. When I want to round something up (down) I import math and use the function math.ceil() (math.floor()). The downside is that ceil() and floor() have no precision "settings". So as an R programmer I would basically just write my own function:

def my_round(x, precision = 0, which = "up"):   
    import math
    x = x * 10 ** precision
    if which == "up":
        x = math.ceil(x)
    elif which == "down":
        x = math.floor(x)
    x = x / (10 ** precision)
    return(x)

my_round(4.00018, 4, "up")

this prints 4.0002

my_round(4.00018, 4, "down")

this prints 4.0001

I can't find a question to this (why?). Is there any other module or function I've missed? Would be great to have a huge library with basic (altered) functions.

edit: I do not talk about integers.

  • Possible duplicate of [Round a Floating Point Number Down to the Nearest Integer?](https://stackoverflow.com/questions/17141979/round-a-floating-point-number-down-to-the-nearest-integer) – U13-Forward Aug 18 '18 at 07:30
  • nope. unfortunately not. I don't talk about integers. –  Aug 18 '18 at 07:32
  • 1
    Julias round function handles this: `round(1.00018, RoundUp, digits=4)` `round(1.00018, RoundDown, digits=4)`, so the julia-lang tag might not be suited. – laborg Aug 18 '18 at 09:12

1 Answers1

2

Check out my answer from this SO post. You should be able to easily modify it to your needs by swapping floor for round.

Please let me know if that helps!


EDIT I just felt it, so I wanted to propose a code based solution

import math

def round2precision(val, precision: int = 0, which: str = ''):
    assert precision >= 0
    val *= 10 ** precision
    round_callback = round
    if which.lower() == 'up':
        round_callback = math.ceil
    if which.lower() == 'down':
        round_callback = math.floor
    return '{1:.{0}f}'.format(precision, round_callback(val) / 10 ** precision)


quantity = 0.00725562
print(quantity)
print(round2precision(quantity, 6, 'up'))
print(round2precision(quantity, 6, 'down'))

which yields

0.00725562
0.007256
0.007255
tafaust
  • 1,457
  • 16
  • 32
  • 1
    Thank you! I just edited your function to comply with my needs and came up with a rewritten solution of mine. –  Aug 18 '18 at 07:42
  • 1
    Perfect! Glad that helped, I just added a code example solution because I couldn't resist the urge to code it myself… :-P Also, I'd be happy if you could select my solution as helpful; thanks! – tafaust Aug 18 '18 at 07:45
  • 1
    Baam! I like your code much more than mine now! Thanks for your help! –  Aug 18 '18 at 09:31
  • 1
    Super happy to hear that! Thank you @MarkusHauschel, a pleasure! – tafaust Aug 18 '18 at 09:33
  • Is there a reason for formatting it into a string variable? –  Aug 19 '18 at 06:53
  • 1
    Actually yes, that was just leftover from my previous SO answer. The `str.format` is used here to take it to the precision we want and then substitute the value with the right precision, but you can just leave it out here or cast it to `float` :-) – tafaust Aug 19 '18 at 07:05