View on GitHub

reads

https://yazansneneh.github.io/reads/

Formatting Numbers for Printing in Python

Precision

x = 4863.4343091          # example float to format

print(f"{x:.6}")          # output: 4863.43

print(f”{x:.3f}”) # 4863.434


## Separators
- seperators are used with large numbers for example 1000000
- they seperate the number to make it easier to read : 1000 000
- how to use them ?
    - add the seperator after ( : ).
    - example: `print(f'{x:,}') # 1,000,000` 
    - it can work with floats as well: `print(f" {x:,.3f}")`

## Percentages
- I can format the number to render as percintage, or add percentage after my number i do the following.
- i swtich f(that stands for fixed) and add percentage(%)
- example:
```python
questions = 30
correct_answers = 23

print(f"You got {correct_answers / questions :.2%} correct!")
# You got 76.67% correct!