What are some best practices for displaying and formatting numerical data retrieved from a database in PHP?

When displaying and formatting numerical data retrieved from a database in PHP, it is important to ensure that the data is properly formatted for readability and consistency. One best practice is to use number_format() function to format numbers with thousands separators and decimal points. Another best practice is to use sprintf() function to format numbers with specific precision and padding.

// Retrieve numerical data from the database
$number = 1234567.89;

// Format the number with thousands separators and decimal points
$formatted_number = number_format($number, 2, '.', ',');

echo $formatted_number; // Output: 1,234,567.89

// Format the number with specific precision and padding
$formatted_number = sprintf("%10.2f", $number);

echo $formatted_number; // Output: 1234567.89