Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to specify floating point digits in python

Shulou Source: shulou.com Published: 2022-06-02 07:17:57 09月18日 Update

This article will explain in detail how to specify floating point digits in python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

1. Round function

Examples are as follows:

> axiom 1.23444

Round (a, 3)

1.234

# here if you specify that the data needs to retain 3 decimal places, but the data itself has only one decimal place

# is the same as the original data

> > baked 1.2

> > round (b, 3)

1.2

# if no precision is specified, the returned result is an integer

> > round (3.5)

four

2. String formatting method

'% [width] [.resume] f'

Width specifies the width of the data, including decimals, and precision specifies precision

If the number of digits specified here is 2 digits, it will be made up even if it is not enough.

> s ='% .2f'

> > baked 1.2

> > s% b

'1.20'

The length specified here is 6 digits, which is aligned to the right by default.

> s ='% 6.2f'

> > s% b

'1.20'

If you need to change the alignment to the left, you can write it as follows

> s ='%-6.2f'

> > s% b

'1.20'

On how to specify floating-point digits in python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data digits decimals points content articles more knowledge articles precision good same not enough writing functions only characters strings widths integers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei MariaDB Docker Linux