Decimal Precision and Scale for Storing Monetary Values
When choosing precision and scale for decimal columns storing monetary values, several factors should be considered.
Efficiency: While char columns of fixed width may be more efficient, decimal columns offer certain advantages. They handle arithmetic operations accurately, allowing for reliable calculations.
Precision and Scale: A commonly used option for monetary values is DECIMAL(19, 4). This configuration provides for 19 total digits, including 4 decimal places. This allows for storage of most currency values with sufficient precision for most applications.
Custom Rounding: Using extra decimal places in storage (e.g., DECIMAL(19, 4)) enables custom rounding algorithms to be implemented, avoiding the default truncation rounding of DECIMAL(p, s) and allowing for compliance with specific accounting rules (e.g., bankers' rounding).
Avoid MONEY Data Type: Avoid using SQL Server's MONEY data type due to potential issues with accuracy and rounding.
Specific Requirements: If special requirements arise, consult with designers or domain experts to determine the appropriate configuration. For instance, some EU intra-state transfers require rounding to five decimal places, necessitating DECIMAL(p, 6) for storage.
The above is the detailed content of What Decimal Precision and Scale Should I Use for Storing Monetary Values in a Database?. For more information, please follow other related articles on the PHP Chinese website!