1

I have print statements of the form:

print("a = ", a, "b = ", b, "c = ", c)

where a, b, and c are floating point numbers. I want to print to three decimal places, and keep the print statements in the form they are in currently, if possible?

Attempt

Following a similar post, I attempted the following:

print(" %.3f a = ", a, "%.3f b = ", b, "%.3f c = ", c)

but this just printed "%.3f" in the print statement. Any suggestions on how to adjust my print statement?

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
Sjoseph
  • 853
  • 2
  • 14
  • 23
  • `print(round(a,3))` – Van Peer Nov 02 '17 at 15:41
  • 2
    I Think that using round is not really what is wanted here even if it does the same result. You can use the string formatting operator like that `print("a = ","%.3f"%a , "b = ","%.3f"%b)` – Peni Nov 02 '17 at 15:46

1 Answers1

5

This will truncate (not round) the printing to 3 decimal places:

print("a = {:.3f}, b = {:.3f}, c = {:.3f} ".format(a, b, c))

[edit:] Using f-strings, the syntax is as follows:

print(f"{a = :.3f}, {b = :.3f}, { c= :.3f}")

If you need to, you can round first.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80