How can the input format of decimal numbers with commas be maintained in a PHP form while ensuring the correct storage and retrieval in a database?

When dealing with decimal numbers with commas in a PHP form, it is important to ensure that the input format is maintained while storing the data correctly in a database. One way to achieve this is by using PHP functions like number_format() to format the input before storing it in the database and using str_replace() to remove commas before performing any calculations. When retrieving the data from the database, you can use number_format() again to display it in the desired format with commas.

// Input from form with decimal numbers containing commas
$input = $_POST['decimal_input'];

// Remove commas and format the number for storage in the database
$number = str_replace(',', '', $input);

// Store the formatted number in the database

// Retrieve the number from the database
$stored_number = 1234567.89; // Example stored number

// Display the retrieved number with commas
$formatted_number = number_format($stored_number, 2, '.', ',');
echo $formatted_number;