mySQL format number output , thousands separator

半腔热情 提交于 2019-12-08 00:57:40

问题


I have a mySQL query which is outputting decimal fields with a comma.

SELECT Metals.Metal, FORMAT(Fixes.GBPam, 3) AS AM, FORMAT(Fixes.GBPpm, 3) AS PM,     
DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id

Fields GBPam and GBPpm are both of type decimal(10,5)

Now I want columns AM and PM to be formatted to 3 decimal places in my sql query - Correct

I want values in the thousands to be formatted as xxxx.xxx and not x,xxx.xxx - Incorrect

Example output from mysql query:

Metal       AM          PM          Date
Gold        1,081.334   NULL    11-09-12
Silver      21.009      NULL    10-09-12
Platinum    995.650     NULL    11-09-12
Palladium   416.700     NULL    11-09-12

Can you see that output for Gold AM is 1,081.334? How can I get it to output 1081.334?

This is a pain in the ass for me because I have to then muck about in PHP to remove the comma. I would prefer to just get mysql to format it correctly.


回答1:


Just use ROUND, this is a numeric function. FORMAT is a string function

ROUND(Fixes.GBPam, 3)



回答2:


you can use replace command for this purpose.

 REPLACE(Fixes.GBPam,',','')

EDIT: With respect to your question you could do something like this:

SELECT Metals.Metal, ROUND(REPLACE(Fixes.GBPam,',',''),3) AS AM,
  ROUND(REPLACE(Fixes.GBPpm,',',''),3) AS PM,     
  DATE_FORMAT(Fixes.DateTime, '%d-%m-%y') AS Date
  FROM Fixes, Metals
WHERE Metals.Id = Fixes.Metals_Id 



回答3:


Use replace function. Whether the field is integer or varchar, it will work.

select replace(Fixes.GBPam,',','.');


来源:https://stackoverflow.com/questions/12368280/mysql-format-number-output-thousands-separator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!