When dealing with numerical data, it's often necessary to display numbers with a specific decimal format. In this case, the goal is to output a number with exactly two decimal places without rounding.
Problem: Find a method to format a number to display two decimal places without rounding the original value.
Example:
Initial Attempts:
The attempts using the FORMAT and CONVERT functions were unsuccessful as they apply rounding instead of truncation.
Solution: Utilize the TRUNCATE Function
To achieve the desired behavior, we employ the TRUNCATE function, which truncates a number to a specified number of decimal places without rounding.
Examples:
Truncation (No Rounding):
TRUNCATE(0.166, 2) -- evaluates to 0.16 TRUNCATE(0.164, 2) -- evaluates to 0.16
Rounding:
The ROUND function can be used for rounding instead of truncation:
ROUND(0.166, 2) -- evaluates to 0.17 ROUND(0.164, 2) -- evaluates to 0.16
Documentation:
The above is the detailed content of How Can I Format a Number to Display Exactly Two Decimal Places Without Rounding?. For more information, please follow other related articles on the PHP Chinese website!