pyspark.sql.functions.format_number
The format_number function is used to format a number as a string. The function takes two arguments: the number to be formatted and the number of decimal places to include.
Here is an example of how to use the format_number function:
Input Data and its Schema
Above you can see the number datatype as double
Use the format_number function to format the numbers with 2 decimal places
Formatted Data and its Schema
You can see the formatted number datatype changed to String.
In this example, we first create a dataframe with a column of numbers, then we use the format_number function to format the numbers with 2 decimal places and rename the column to formatted_number, the output will be a dataframe with two columns: name, and formatted_number, where the second column will contain the numbers rounded to 2 decimal places.
This will formats the number X to a format like ‘#,–#,–#.–’, rounded to d decimal places with HALF_EVEN round mode, and returns the result as a string.
Spark important urls