Home > Enterprise >  SQL Cannot Format Numbers and Decimal (Always Automatically Rounding Up)
SQL Cannot Format Numbers and Decimal (Always Automatically Rounding Up)

Time:06-20

I want to confirm if SQL still can do format numbers from 123456.68 to 123,456.68 like this Query from https://www.mssqltips.com/

    SELECT FORMAT(1234.5678, 'N', 'en-us')

since my SQL always rounded it like this my MySQL query

I tried several query I found on other stack overflow like

SELECT FORMAT(1234.56,'#,0.0000')

and

SELECT Format(1234.56,'#,##0')

then

SELECT FORMAT(123456.789, '###,###,###.##', 'en-us')

But it still rounded the decimal... Is it will always rounded the decimal or there's some setting I need to do? Is there any way to do it ? I am using mySQL. Thank you!

CodePudding user response:

Simply:

SELECT FORMAT(123456.68,2);

Parameters : This function generally accepts three parameters.

N – The number which is to be formatted.

D – The number of decimal places to which the number is round off.

locale – It’s an optional parameter, which decides a thousand separators and grouping between separators. By default, en_US locale is present in MySQL.

Returns : The function formats the given number, round off it to a certain decimal place, and return the number in the form of string.

Check for more info: https://www.geeksforgeeks.org/format-function-in-mysql/

Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4aa3cbf11cc05f2e1accc4fa6bcd20a1

  • Related