Remove trailing zeros in decimal value with changi

2020-01-30 07:47发布

I have a procedure I am doing that displays odds but the client wants only significant digits to be shown. So, 1.50 would show as '1.5' and 1.00 would show as '1'.

How can I get MySQL to not display trailing zeros;

i.e. in the database:

Odds
1.500
23.030
2.000
4.450

would display as

1.5
23.03
2
4.45

Thanks for any help

标签: mysql
14条回答
虎瘦雄心在
2楼-- · 2020-01-30 08:25

EDIT: I would use the answer below by Christopher McGowan instead - adding 0 to the value, which is better, instead.


It's important to check there is actually a decimal point if doing trimming.

So I think you'd want to use:

SELECT TRIM(TRAILING '.' FROM TRIM(TRAILING '0' from yourfield)) AS yourfield
FROM yourtable
WHERE yourfield LIKE '%.%'
查看更多
小情绪 Triste *
3楼-- · 2020-01-30 08:26
SELECT TRIM(TRAILING '0' FROM yourodds)
FROM ...

Docs for the TRIM function.

查看更多
登录 后发表回答