Currency format

Asked

Viewed 5,997 times

1

To be complete I could not enter a FORMAT(Value,2) including in CONCAT?

SELECT * , CONCAT('de ', Valor, ' por ', desconto) AS promocao  
FROM (SELECT Valor, Valor - Valor * 20/100 AS desconto  
FROM comissao 
ORDER BY comissao.Valor DESC) t; 

On the page the result of the "promotion" column of CONCAT is left without formatting as for example: "of 14230 by 11384' would be better 14.230,00 and the discount of 20% getting 11.384,00.

  • Was the answer helpful? Don’t forget to mark to accept it. So you can use it if someone has a similar question!

1 answer

3

You can use the function FORMAT as follows:

SELECT t.*,
       CONCAT('de ', FORMAT(t.valor, 2, 'de_DE'), ' por ', FORMAT(t.desconto, 2, 'de_DE')) AS promocao  
  FROM (SELECT c.Valor as valor,
               c.valor - c.valor * 20/100 AS desconto
          FROM comissao c
         ORDER BY c.valor DESC) t; 

The result of the promotion column would be:

of 14,230,00 by 11,384,00

As to function:

FORMAT(X,D[,locale])

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.

The optional third Parameter Enables a locale to be specified to be used for the result number’s decimal point, thousands separator, and grouping between separators. ... . If no locale is specified, the default is 'en_US'.

Or in free translation:

Format a number X to a format '#,###,##. #' rounding to the decimal places D, and return the result as a string. If D is 0, the result has no decimal places.

The third (optional) parameter enables the locale specification used to determine the decimal separator, the thousands separator and the grouping between result separators. ... . If no location is specified, the default is 'en_US'.

I used the locality de_DE because Germany uses the international standard that is the same that we use and the pt_BR did not bring the separator of thousands.

  • Thank you very much it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.