在MySQL中,我只想在数字中添加千位分隔符,如1234.23234、242343.345345464、232423.22,并格式化为“1,234.23234”、“242,343.345345464”、“232,423.22”,使用format函数需要指定小数位数,有吗还有其他函数可以格式化小数位数未知的数字吗?对于1234.23234,我不想得到像1234.2323400000或1234.23这样的结果,只想得到1,234.23234。
最佳答案
按照建议,分割字符串,删除尾随零,格式化小数点之前的数字,并考虑到根本不存在小数的可能性,例如
set @a = 1234.56;
select
case when instr(@a,'.') > 0 then
concat(
format(substring_index(@a,'.',1),'###,###,###'),
'.',
trim(trailing '0' from substring_index(@a,'.',-1))
)
else
format (@a,'###,###,###')
end formatted
关于MySQL 格式数字,小数位数未知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72137059/