Home >Database >Mysql Tutorial >How Can I Truncate Numbers to Exactly Two Decimal Places in MySQL Without Rounding?
Truncating Numbers to Preserved Exact Values with Two Decimal Places
When working with numbers, it's often necessary to format them to a specific precision. In this particular case, you're interested in displaying numbers with exactly two decimal places, without rounding.
The key to achieving this is to use the TRUNCATE function, which removes digits from the end of a number without performing any rounding. For example:
TRUNCATE(2229.999, 2)
This expression would result in "2229.99", preserving the exact value without rounding it up to "2230.00".
To understand the difference from rounding, consider the following examples:
ROUND(0.166, 2) -- Results in 0.17 (rounded up) TRUNCATE(0.166, 2) -- Results in 0.16 (truncated)
As you can see, the TRUNCATE function maintains the original value without introducing any rounding errors.
Here is the official MySQL documentation for the TRUNCATE function:
https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate
The above is the detailed content of How Can I Truncate Numbers to Exactly Two Decimal Places in MySQL Without Rounding?. For more information, please follow other related articles on the PHP Chinese website!