0

I need to write a decimal with 0/zero precision, but I want it to include the decimal point. I am using f-strings.

This is what I have tried:

test = f"{8:3.0f}"
print(test)
Out   8

As well as just doing:

test = f"{8:3.0f}" + "."
print(test)
Out   8.

And

test = f"{8:2.0f}" + "."
print(test)
Out  8.

The last is what I want, but I want the decimal point "." to be included as one of the 3 characters as this is a format specific issue. So when the code is read, it reads a 3 size float that is ' 8.'

Is there a way to do this?

fallfish
  • 15
  • 6
  • 1
    So what's the issue? You have posted two other ways of getting the desired output, so what's the question here? – Amit Amola Jun 07 '19 at 13:20
  • @AmitAmola The problem is that those methods were giving strings as outputs and probably desired output is float – Igor sharm Jun 07 '19 at 13:23
  • You won't get an output to `print` as a float @Igorsharm. The `print` function automatically type casts ALL objects to strings. You can pass a float but regardless it will end up as a `str`. This is basically how pythons print [is implemented](https://bitbucket.org/pypy/pypy/src/ed43c2dc0a42843bbf9637b14a2c30dfe2785295/pypy/module/__builtin__/app_io.py?at=default#cl-78). – Error - Syntactical Remorse Jun 07 '19 at 13:24
  • I got it. Let me try. – Amit Amola Jun 07 '19 at 13:24
  • 3
    It would be shorter, look better, and perhaps be more clear if you included the decimal point in the f-string rather than appending it later: `test = f"{8:2.0f}."` – Rory Daulton Jun 07 '19 at 13:35

0 Answers0