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

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-28 21:36:11350browse

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

Formatting Numbers to Two Decimal Places Without Rounding

In order to display a number with two decimal places without rounding the original number, it is possible to use the TRUNCATE function. This function removes all decimal places beyond the specified number.

Example

To truncate a number to two decimal places, the following syntax can be used:

TRUNCATE(number, 2)

For instance, to truncate the number 2229.999 to two decimal places, the following expression can be used:

TRUNCATE(2229.999, 2)

The result of this expression will be 2229.99.

Other Options

In addition to TRUNCATE, the ROUND function can also be used to format numbers to a specified number of decimal places. However, ROUND rounds the number to the nearest decimal place, which may not be desirable in some cases.

Documentation

For more information on the TRUNCATE function, refer to the MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate

For more information on the ROUND function, refer to the MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_round

The above is the detailed content of How to Format Numbers to Two Decimal Places Without Rounding in MySQL?. 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