Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print whole number without zeros after decimal point?

I’m trying to print a whole number (such as 39 for example) in the following format: 39.
It must not be a str type object like ’39.’ for example, but a number

e. g. n = 39.0 should be printed like 39.

n = 39.0
#magic stuff with output
39.

I tried using :.nf methods (:.0f apparently — didn’t work), print(float(39.)) or just print(39.)
In the first case, it looks like 39, in the second and third 39.0
I also tried float(str(39) + ‘.’) and obviously it didn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sorry, if it’s a stupid question, I’ve been trying to solve it for several hours already, still can’t find any information.

>Solution :

From Format Specification Mini-Language (emphasis mine):

The '#' option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value. For float and complex the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result.

>>> n=39.0
>>> print(f'{n:#.0f}')
39.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading