Built-in format
Function
- The
format
function is used to produce a numeric string version of a floating-point value with a specific number of decimal places. - It allows controlling the precision and formatting of numeric values in string output.
Examples:
Rounding to Two Decimal Places:
![](https://bscbcanotes.wordpress.com/wp-content/uploads/2024/05/image-17.png?w=1024)
The format specifier '.2f'
rounds the result to two decimal places.
Scientific Notation:
![](https://bscbcanotes.wordpress.com/wp-content/uploads/2024/05/image-18.png?w=444)
The value is formatted in scientific notation with six decimal places of precision.
Formatting with Comma Separators:
![](https://bscbcanotes.wordpress.com/wp-content/uploads/2024/05/image-19.png?w=664)
Comma separators are added to the result for better readability.