MySQL rounding function ROUND (x), ROUND (× x, Y), and truncate (x, y)

Source: Internet
Author: User
Tags reserved

MySQL Rounding function round (x)

The ROUND (x) function returns the integer closest to parameter x, rounding the X value.

Instance:

The operand is rounded using the round (x) function. The SQL statements are as follows:

Mysql>select ROUND ( -2.34), ROUND ( -4.56), ROUND (2.34), ROUND (4.56);

The execution results of the ROUND (x) function are as follows:

The results from the code execution show that after rounding, only the integer portion of each value is retained.

MySQL Rounding function round (x, y)

The ROUND (x, y) function returns the number closest to the parameter x, whose value remains at the Y-bit after the decimal point, and if Y is negative, the X value is preserved to the left Y-bit of the decimal point.

Instance:

The operand is rounded using the round (x, Y) function, and the result is reserved for the Y-bit after the decimal point. The SQL statements are as follows:

Mysql>select ROUND (3.45,1), ROUND (3.45,0), ROUND (123.45,-1), ROUND (167.8,-2);

The execution results of the ROUND (x, y) function are as follows:

ROUND (3.45,1) retains 1 bits after the decimal point, rounding the result of 3.5;round (3.45,0) retains 0 digits after the decimal point, that is, returning the rounded integer value, ROUND (123.45,-1) retains 1 digits to the left of the decimal point, which is 1 bits to the left of the decimal point, The 1 bits are replaced with 0, and the return value is 120;round (167.8,-2), leaving 2 digits to the left of the decimal point, which is 2 bits to the left of the decimal point, two in lieu of 0, and returns the rounded value of 200.

MySQL rounding function truncate (x, y)

The TRUNCATE (x, y) function returns the number x that is removed to the Y-bit after the decimal point. If Y has a value of 0, the result does not have a decimal point or a fractional part. If Y is set to a negative number, the value of all lows after the beginning of the Y-bit of the decimal point is truncated (zeroed).

Instance:

The operand is rounded using the truncate (x, y) function, and the result is reserved for the Y-bit after the decimal point. The SQL statements are as follows:

Mysql>select TRUNCATE (2.34,1), TRUNCATE (4.56,1), TRUNCATE (4.56,0), TRUNCATE (56.78,-1);

The execution results of the TRUNCATE (x, y) function are as follows:

TRUNCATE (2.34,1) and TRUNCATE (4.56,1) retain 1 digits after the decimal point, with the return value 2.3 and 4.5;truncate (4.56,0) Returning the integer part 4;truncate (56.78,-1) Truncate the value after the 1th digit to the left of the decimal point, and set the 1-digit number of the integer part to 0, resulting in 50.

Tips

The ROUND (x, y) function is rounded when the value is intercepted, and the truncate (x, y) function intercepts the value directly and does not round it.

MySQL rounding function ROUND (x), ROUND (× x, Y), and truncate (x, y)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.