# Format a floating number to fixed width in Python

In this Python tutorial, we will learn how to format a floating number to a fixed width.

## Format a floating number to fixed width using % operator

The % operator can be used as a formatting operator. Such that it can be possible to return fixed width from a floating point number. We need to specify the width as an integer (number of decimals) to be returned.

Syntax:

```'%.nf'%value
```

Where n specifies the width and value is the floating point number. It returns a fixed width float point number.

Example 1:

Consider the floating number – 12.456734455 and return 6,8 and 2 decimals separately.

```# Consider a float value with 9 decimals
value = 12.456734455

# Get upto 6 decimals
print ('value upto 6 decimals - %.6f' % value)

# Get upto 8 decimals
print ('value upto 8 decimals - %.8f' % value)

# Get upto 2 decimals
print ('value upto 2 decimals - %.2f' % value)
```

Output:

```value upto 6 decimals - 12.456734
value upto 8 decimals - 12.45673445
value upto 2 decimals - 12.46
```
1. We specified width as 6. so it returned only 6 decimals – 12.456734
2. We specified width as 8. so it returned only 6 decimals – 12.45673445
3. We specified width as 2. so it returned only 6 decimals – 12.45

Example 2:

Consider the 5 floating point numbers in a list and return the result in a fixed width.

```# Consider a float values with different decimals in a list
values = [12.456734455,5.6789,9.232345,7.89998,8.00122]

# Get upto 3 decimals
for i in values:
print ('value upto 3 decimals - %.6f'%i)

# Get upto 2 decimals
for i in values:
print ('value upto 2 decimals - %.2f'%i)
```

Output:

```value upto 3 decimals - 12.456734
value upto 3 decimals - 5.678900
value upto 3 decimals - 9.232345
value upto 3 decimals - 7.899980
value upto 3 decimals - 8.001220
value upto 2 decimals - 12.46
value upto 2 decimals - 5.68
value upto 2 decimals - 9.23
value upto 2 decimals - 7.90
value upto 2 decimals - 8.00
```

We returned floating numbers in a list with widths of 3 and 2 separately.

## Format a floating number to fixed width using round()

The round() function is used to round the given float value to a specified width. It rounds to the nearest value.

Syntax:

```round(value,width)
```

It returns a fixed width float point number.

Parameters:
1. value specifies float value
2. width specifies the number of decimals to be returned.

Example:

In this example, we will round the value – 12.456734455 to 6,8 and 2 separately.

```# Consider a float value with 9 decimals
value = 12.456734455

# Get upto 6 decimals
print ("value upto 6 decimals",round(value,6))

# Get upto 8 decimals
print ("value upto 8 decimals",round(value,8))

# Get upto 2 decimals
print ("value upto 2 decimals",round(value,2))
```

Output:

```value upto 6 decimals 12.456734
value upto 8 decimals 12.45673445
value upto 2 decimals 12.46
```
1. 12.456734455 is rounded to 12.456734 by specifying a width as 6.
2. 12.456734455 is rounded to 12.45673445 by specifying a width as 8.
3. 12.456734455 is rounded to the nearest value – 12.46 by specifying a width as 2.

## Format a floating number to fixed width using format()

The format() is used to round the given float value to a specified width. It rounds to the nearest value.

Syntax:

```"{:.nf}".format(value)
```

Where n specifies the width and value is the floating point number.

Example:1

In this example, we will format the value – 12.456734455 to 4,8, and 2 separately.

```# Consider a float value with 9 decimals
value = 12.456734455

# Get upto 4 decimals
print ("value upto 6 decimals - {:.4f}".format(value))

# Get upto 8 decimals
print ("value upto 8 decimals - {:.8f}".format(value))

# Get upto 2 decimals
print ("value upto 2 decimals - {:.2f}".format(value))
```

Output:

```value upto 6 decimals - 12.4567
value upto 8 decimals - 12.45673445
value upto 2 decimals - 12.46
```
1. 12.456734455 is formatted to 12.4567 by specifying a width as 4.
2. 12.456734455 is formatted to 12.45673445 by specifying a width as 8.
3. 12.456734455 is formatted to the nearest value – 12.46 by specifying a width as 2.

Example:2

In this example, we will format the value – 89.01123443535 to 6,8 and 2 separately.

```# Consider a float value with 11 decimals
value = 89.01123443535

# Get upto 6 decimals
print ("value upto 6 decimals - {:.6f}".format(value))

# Get upto 8 decimals
print ("value upto 8 decimals - {:.8f}".format(value))

# Get upto 2 decimals
print ("value upto 2 decimals - {:.2f}".format(value))
```

Output:

```value upto 6 decimals - 89.011234
value upto 8 decimals - 89.01123444
value upto 2 decimals - 89.01
```

## Summary

In this Python tutorial, we have seen how to format a floating point number to a fixed width using round(), format and % operator. The round() and format() methods operates with the same functionality. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top