0
def competitiveEating(t, width, precision):
    return str(round(t, precision)).center(width).rstrip('.0')

so I want to remove trailing .0s from whole numbers given through my input t thats being rounded to the precision number, when I do r.strip after .center nothing happens, I don't know much so I'm not sure if you can even put a .function after another .function (the rstrip after center). I was reading about '{0:g}'.format(float(21)) here's a link (the solved question doesn't work in this situation). I honestly don't even know how to add or use the {0:g}'.format(float(21)) part..

Community
  • 1
  • 1
oso9817
  • 117
  • 1
  • 3
  • 14
  • 1
    probably python. tag with programming language please – Bernd Elkemann Dec 31 '16 at 05:50
  • rstrip first, then call center, else a short number will be space padded on left and right sides, and rstrip('.0') won't find any '.'s or '0's to strip, just the trailing spaces. – PaulMcG Dec 31 '16 at 06:42

2 Answers2

0

Here's a way to do that:

def competitiveEating(t, width, precision):
    text = str(round(t, precision))
    if text.endswith('.0'):
        text = text[:-2]
    return text.center(width)
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
  • Thanks but r.strip('.0') removes all instances of '.' and '0' – oso9817 Dec 31 '16 at 05:58
  • You're right. I believe I have a corrected version now. – Chai T. Rex Dec 31 '16 at 06:11
  • I think the OP wants to strip all 0's after a decimal, so "1.000" should become "1", and this will fail since it does not end with '.0'. – PaulMcG Dec 31 '16 at 06:41
  • @PaulMcGuire The input `t` is a number, not a string, as shown by it being put through `round`, which doesn't take strings. `str(1.000)` is `"1.0"`, so it doesn't have the problem you mention. – Chai T. Rex Dec 31 '16 at 07:32
0
def competitiveEating(t, width, precision):
return ("{:."+str(precision)+"f}").format(t).center(width)

The way you are rounding up won't work for things like precision = 0, and if it is something like 1000.00, you will end up with 1.

And if you don't care about this, you could do it like this:

def competitiveEating(t, width, precision):
    return str(round(t, precision)).rstrip('.0').center(width)
Noah Han
  • 141
  • 1
  • 6