Left-justifying floating-point number output

Question:
Is the “-” in the format used below supposed to left-justify the number?

Code:

This section is just generating numbers to demonstrate the problem.

def neuralNetwork(input, weight):
prediction = input * weight
return prediction
​weight = 0.5
goalPrediction = 0.8
input = 0.5
for iteration in range(124):
prediction = neuralNetwork(input, weight)
error = (prediction - goalPrediction) ** 2
directionAndAmount = (prediction - goalPrediction) * input
weight = weight - directionAndAmount

Expect the “-” before “11.6g” to left-justify number in 11 character field but it does not.

print(f"Error:{error:-11.6g} Prediction:{prediction:-11.6f}" + \
      f" Iteration:{iteration:4d}")

Output:
Error: 0.3025 Prediction: 0.250000 Iteration: 0
Error: 0.170156 Prediction: 0.387500 Iteration: 1
Error: 0.0957129 Prediction: 0.490625 Iteration: 2
Error: 0.0538385 Prediction: 0.567969 Iteration: 3
Error: 0.0302842 Prediction: 0.625977 Iteration: 4
Error: 0.0170348 Prediction: 0.669482 Iteration: 5
Error: 0.0095821 Prediction: 0.702112 Iteration: 6
Error: 0.00538993 Prediction: 0.726584 Iteration: 7
Error: 0.00303184 Prediction: 0.744938 Iteration: 8
Error: 0.00170541 Prediction: 0.758703 Iteration: 9
Error:0.000959292 Prediction: 0.769028 Iteration: 10
Error:0.000539602 Prediction: 0.776771 Iteration: 11

The documentation has the Format mini-language.

“-” is already the default and indicates that you only want a sign shown for negative numbers, not positive.
“<” is for left alignment.

Hi William,

Thank you for showing your code, but it is better if you can cut out all

the extraneous stuff that it irrelevant to your question.

If you are asking about formatting a number, all you need is two lines:

error = 0.05

print(f"Error:|{error:-11.6g}|"

which prints “Error:| 0.05|”, clearly not left justified. And we

can see that without needing to care about neural networks or having to

wonder what your data is.

To left or centre justify the number:

print(f"Error:|{error:<11.6g}|")

# --> prints "Error:|0.05       |"



print(f"Error:|{error:^11.6g}|")

# --> prints "Error:|   0.05    |"

Alas, there is no format code for aligning on the decimal point.

Thanks, “<” worked.

Thanks for the documentation link.

Thanks again for you quick and accurate response.

Thanks for your quick response.