Managing Data Conversions II

Learn how to format numbers and manage the process of data conversion.

We worked to convert our string data back into our original records. We used PHP’s intval and floatval functions to convert our strings into PHP data types. Converting our initial data into strings and then from strings back into floats and integers are examples of data conversion. It is relatively rare that data will be stored strictly as-is without any conversion process.

Formatting numbers when converting invoice data

Let’s revisit the idea of converting our invoice data into a fixed-width string format. Still, instead of simply encoding everything as a string, let’s decide that all financial data, such as our total, will be stored with trailing zeroes up to two digits of precision. We can accomplish this by making use of PHP’s number_format function:

Get hands-on with 1400+ tech skills courses.