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
Release: 2024-12-28 21:36:11
Original
358 people have browsed it

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)
Copy after login

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

TRUNCATE(2229.999, 2)
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template