How can PHP developers ensure consistent decimal point formatting when exporting data to a CSV file from a database query?

When exporting data to a CSV file from a database query, PHP developers can ensure consistent decimal point formatting by using the number_format() function to format the decimal values to the desired precision before writing them to the CSV file.

// Assume $data is an array of data retrieved from a database query

// Open a file handle for writing
$file = fopen('export.csv', 'w');

// Loop through the data and format decimal values before writing to the CSV file
foreach ($data as $row) {
    // Format decimal values to 2 decimal places
    $formattedRow = array_map(function($value) {
        return is_numeric($value) ? number_format($value, 2) : $value;
    }, $row);

    // Write the formatted row to the CSV file
    fputcsv($file, $formattedRow);
}

// Close the file handle
fclose($file);