Home >Database >Mysql Tutorial >How to Format Numbers to Two Decimal Places in MySQL Without Rounding?

How to Format Numbers to Two Decimal Places in MySQL Without Rounding?

Susan Sarandon
Susan SarandonOriginal
2024-12-04 15:36:12933browse

How to Format Numbers to Two Decimal Places in MySQL Without Rounding?

Formatting Numbers to Two Decimal Places Without Rounding

To output a number with two decimal places without rounding the original number, you can utilize the TRUNCATE function in MySQL. This function removes any digits beyond the specified precision, effectively truncating the numerical value.

Syntax:

TRUNCATE(number, precision)

Example:

To format the number 2229.999 with two decimal places without rounding, you can use the following query:

SELECT TRUNCATE(2229.999, 2);

This query will output:

2229,99

The TRUNCATE function is useful for truncating numerical values to the desired precision, ensuring that the original number is not altered by rounding.

Note:

If you wish to round the number instead of truncating it, you can use the ROUND function. However, this function will round the number to the nearest whole number, so the result may differ from the TRUNCATE function.

The above is the detailed content of How to Format Numbers to Two Decimal Places in MySQL Without Rounding?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn