 # Python Number Format

Formatting numbers in Python is necessary to display numbers in a specific format.

Formatting can be used when you want to round off a number to a specific number of decimal places, or when you want you number to be separated by commas.

To format numbers we can use f-string or format() function. ## 1. Round float to nearest integer

Let's say we have a float number and we want to round it off to the nearest integer.

We can do this in many ways. Here we have discussed 3 ways to do this.

``````num = 3.141592653589793

# Method 1:
print("Nearest integer of num is {:.0f}".format(num))

# Method 2:
print(f"Nearest integer of num is {num:.0f}")

# Method 3:
print(f"Nearest integer of num is {round(num)}")``````

Output:

```Nearest integer of num is 3
Nearest integer of num is 3
Nearest integer of num is 3```

## 2. Round float to 2 decimal places

Suppose you want to round off a float number to 2 decimal places or any other number of decimal places.

To do this you can use format specifiers .2f, .3f, or .nf for n decimal places.

``````num = 3.141592653589793

# Method 1:
print("num round to 2 decimal is {:.2f}".format(num))

# Method 2:
print(f"num round to 2 decimal is {num:.2f}")``````

Output:

```num round to 2 decimal is 3.14
num round to 2 decimal is 3.14```

## 3. Format percentage value in numbers

Format a percentage sign (%) after rounding float to 2 decimal places. For this, you can simply use the % sign after the .2f format specifier.

``````num = 3.141592653589793

# Method 1:
print("{:.2f}%".format(num))

# Method 2:
print(f"{num:.2f}%")``````

Output:

```3.14%
3.14%```

## 4. Format currency value in numbers

To format an amount as a currency value, you can use the locale module.

locale module lets us work without having to worry about the conventions of a specific locale.

We can use this module to format currency values.

After importing it first set the locale to your country's locale. For example, `locale.setlocale(locale.LC_ALL, 'en_US')` for US locale.

Then use `locale.currency()` function to format the currency value.

``````import locale

amount = 525000

# set locale to US
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
# format currency value
doller = locale.currency(amount, grouping=True)
print(doller)

# set locale to India
locale.setlocale(locale.LC_ALL, 'en_IN.UTF-8')
# format currency value
rupee = locale.currency(amount, grouping=True)
print(rupee)``````

Output:

```\$525,000.00
₹5,25,000.00```

## 5. Add comma separator in numbers

Comma separated numbers are easier to read. To add comma separator in numbers use the format specifier {:,} or {:,.nf} for n decimal places.

``````num = 1234567890.23456789

# Method 1:
print("{:,}".format(num))

# Method 2:
# comma separated with 2 decimal places
print(f"{num:,.2f}")``````

Output:

```1,234,567,890.2345679
1,234,567,890.23```

## 6. Format numbers in scientific notation

Scientific notation is a way of expressing numbers that are too large or too small to be conveniently written in decimal notation.

For example, 1,000,000,000 can be written as 1 x 109 and in scientific notation it is written as 1e9 or 1E9.

To format a number in scientific notation use the format specifier e or E.

``````num = 123456789

# Method 1:
print("{:e}".format(num))

# Method 2:
print(f"{num:E}")``````

Output:

```1.234568e+09
1.234568E+09```

Zeros are added to the left of the number to make it of a specific length.

Use the format specifier {0:>n} or {0:0n} to make the number of length n.

``````num = 123

# Method 1:
print("{:0>6d}".format(num))

# Method 2:
print(f"{num:06d}")``````

Output:

```000123
000123```

## 8. Format Number to Other Base

There are few format specifiers to format a number to other bases.

For example:

• :b for binary
• :o for octal
``````num = 12345

# binary Format
print(f"{num} in binary is {num:b}")

# octal Format
print(f"{num} in octal is {num:o}")

```12345 in binary is 11000000111001