MySql formatting decimal with two decimal places

method one:

SELECT FORMAT(12562.6655,2);

Result: 12,562.67

SELECT FORMAT(12332.1,4);

Result: 12,332.1000 (the decimal will be filled with 0 if there are no digits)

See documentation: Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part. When the integer part exceeds three digits, it is separated by commas, and the returned result is of type string.

Method 2

select truncate(4545.1366,2);

Result: Result: 4545.13 (truncated directly, not rounded)

way three

select convert(4545.1366,decimal(10,2));

Result: 4545.14 The
convert() function will round the fractional part, decimal(10,2): It means that the final result of the integer part plus the number of decimal places is less than or equal to 10, and the number of decimal places is 2

way four

round: Returns a numeric expression rounded to the specified length or precision.

Syntax: ROUND ( numeric_expression , length [ , function ] )
Parameters:
numeric_expression : An expression of the exact numeric or approximate numeric data type category (except for the bit data type).

length: is the precision with which numeric_e-xpression will be rounded. length must be tinyint, smallint, or int. When length is positive, numeric_e-xpression rounds to the number of decimal places specified by length. When length is negative, numeric_e
-xpression is rounded to the left of the decimal point as specified by length.

function: is the type of operation to perform. function must be tinyint, smallint, or int. If function is omitted or the value of function is 0 (the default), numeric_expression will be rounded up. Numeric_expression is truncated when a value other than 0 is specified.

Return Type: Returns the same type as numeric_e-xpression.
ROUND always returns a value. If length is negative and greater than the number of digits before the decimal point, ROUND will return 0.
Example

select ROUND(748.58, -4);

Result: 0
When length is negative, ROUND will return a rounded numeric_e-xpression regardless of the data type.
Example

ROUND(748.58, -1);
ROUND(748.58, -2);
ROUND(748.58, -3);

Result:
750
700
1000

select ROUND(4545.1366,2); 

Result: 4545.15

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325703445&siteId=291194637