How can one ensure that decimal places are retained when fetching values from a database using PHP, specifically when dealing with float or real data types?

When fetching values from a database using PHP, particularly when dealing with float or real data types, it's important to ensure that decimal places are retained. To achieve this, you can use the PHP `number_format()` function to format the retrieved values to the desired decimal precision.

// Connect to the database
$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password");

// Fetch data from the database
$stmt = $pdo->query("SELECT price FROM products");
while ($row = $stmt->fetch()) {
    $price = number_format($row['price'], 2); // Retain 2 decimal places
    echo $price . "<br>";
}