How do I concatenate strings of rounded numbers python?

Viewed 434

I'm having trouble figuring out an error in how I'm concatenating a string in python.

The goal is to format numbers into a string which I can print with a consistent length.

I've written the following code:

def numPrint(number,roundplace):
    num = round(number, roundplace)
    if num > 0:
        output = ('+' + str(num))
    elif num < 0:
        output = (str(num))
    else:
        output = (' 0.' + '0' * roundplace)    

    if len(output) < (3 + roundplace):
        output2 = (output + '0')
    else:
        output2 = output

    return output2

print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))

I expect it to print:

+0.001
 0.000
-0.002
-0.010
+0.100

however, I'm getting

+0.001
 0.000
-0.002
-0.010
+0.10

How do I add "0"'s to the last one to get it to work?

7 Answers

You just forgot to multiply the zeros for output2:

if len(output) < (3 + roundplace):
    output2 = (output + ('0'*(3 + roundplace - len(output))))
else:
    output2 = output

Or if you don't mind using built-in function:

output2 = output.ljust(3 + roundplace, '0')

Try this:


def num_print(number, roundplace):
    sign = '+' if number != 0 else ' '
    output = '{num:{sign}.{rp}f}'.format(num=number, sign=sign, rp=roundplace) # python3
#     output = f'{number:{sign}.{roundplace}f}' # python 3.6 +
    return output

+0.001
 0.000
-0.002
-0.010
+0.100

You can use string formatting if you just want to display the numbers.

def format_number(number, minimum_digits=1, decimal_places=2):
    format_string = '{{: {}.{}f}}'.format(minimum_digits, decimal_places)
    result = format_string.format(number)
    if number:
        result = result.replace(' ', '+', 1)  # Technically you can use '+' above but your desired output requires zero to have no sign
    return result

print(format_number(0.001, minimum_digits=1, decimal_places=3))
print(format_number(0, minimum_digits=1, decimal_places=3))
print(format_number(-0.0019, minimum_digits=1, decimal_places=3))
print(format_number(-0.01, minimum_digits=1, decimal_places=3))
print(format_number(0.1, minimum_digits=1, decimal_places=3))
+0.001
 0.000
-0.002
-0.010
+0.100

Try using the .format() string method, it's really useful and handy.

def numPrint(number,roundPlace):
   return "{{:+.{}f}}".format(roundPlace).format(number)

This will work:

def numPrint(number,roundplace):
    num = round(number, roundplace)
    if num > 0:
        output = ('+' + str(num))
    elif num < 0:
        output = (str(num))
    else:
        output = (' 0.' + '0' * roundplace)    

    while len(output) < (3 + roundplace): # previously you stopped after adding one trailing 0
        output = (output + '0')

    return output

print(numPrint(0.001, 3))
print(numPrint(0, 3))
print(numPrint(-0.0019, 3))
print(numPrint(-0.01, 3))
print(numPrint(0.1, 3))

For Python 3.6+ you may use an f-string:

def num_print(number, round_place):
    return f"{number:{'+' if number else ' '}.{round_place}f}"

(I intentionally changed the names of your function and its second parameter from numPrint and roundPlace to num_print and round_place to be in concordance with PEP 8 - Style Guide: Function and Variable Names.)

Related