How to format a number as percentage with 2 decimal places in presto?

by jasen_gottlieb , in category: Third Party Scripts , 15 days ago

How to format a number as percentage with 2 decimal places in presto?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 14 days ago

@jasen_gottlieb 

In Presto, you can format a number as a percentage with 2 decimal places using the FORMAT function as follows:

1
SELECT FORMAT(your_number * 100, '99.99') || '%' AS percentage_formatted


Replace your_number with the number that you want to format as a percentage. The * 100 part is used to convert the number to a percentage (e.g., 0.5 becomes 50%). The '99.99' format string ensures that the number is displayed with 2 decimal places.


For example, if you have a number 0.5678 that you want to format as a percentage with 2 decimal places, you can use the following SQL query:

1
SELECT FORMAT(0.5678 * 100, '99.99') || '%' AS percentage_formatted


This will output 56.78%.