MySQL 小数位数未知的格式数字
MySQL format number with unknown number of decimal places
在MySQL中,我只想在1234.23234、242343.345345464、232423.22等数字中添加千位分隔符,格式为“1234.23234”、“242343.345345464”、“232423.22”,使用格式函数需要指定小数位数,有没有其他函数可以格式化小数位数未知的数字?对于 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好像没有这个功能。您可能需要根据 FORMAT() plus some string manipulation to remove trailing zeroes after the comma, for example using REGEXP_REPLACE() 编写自定义函数。 FORMAT()
中使用的默认语言环境是 en_US
,这似乎是您想要的语言环境,因此如果您需要不同的语言环境,您可以省略它或提供您自己的语言环境。
WITH sample_data (sample_number) AS (
SELECT NULL
UNION ALL SELECT 0
UNION ALL SELECT 0.00001
UNION ALL SELECT 100.01
UNION ALL SELECT 100.0102
UNION ALL SELECT 100.012300456
UNION ALL SELECT 1000
UNION ALL SELECT 123456789.87654321
UNION ALL SELECT -56500.333
)
SELECT
sample_number,
REGEXP_REPLACE(
FORMAT(sample_number, 999),
'(\.\d*[1-9])(0+$)|(\.0+$)',
''
) AS USA,
-- Replace \. with , for locales that use comma as decimal separator:
REGEXP_REPLACE(
FORMAT(sample_number, 999, 'de_DE'),
'(,\d*[1-9])(0+$)|(,0+$)',
''
) AS Germany
FROM sample_data;
sample_number
USA
Germany
NULL
NULL
NULL
0.000000000
0
0
0.000010000
0.00001
0,00001
100.010000000
100.01
100,01
100.010200000
100.0102
100,0102
100.012300456
100.012300456
100,012300456
1000.000000000
1,000
1.000
123456789.876543210
123,456,789.87654321
123.456.789,87654321
-56500.333000000
-56,500.333
-56.500,333
在MySQL中,我只想在1234.23234、242343.345345464、232423.22等数字中添加千位分隔符,格式为“1234.23234”、“242343.345345464”、“232423.22”,使用格式函数需要指定小数位数,有没有其他函数可以格式化小数位数未知的数字?对于 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好像没有这个功能。您可能需要根据 FORMAT() plus some string manipulation to remove trailing zeroes after the comma, for example using REGEXP_REPLACE() 编写自定义函数。 FORMAT()
中使用的默认语言环境是 en_US
,这似乎是您想要的语言环境,因此如果您需要不同的语言环境,您可以省略它或提供您自己的语言环境。
WITH sample_data (sample_number) AS (
SELECT NULL
UNION ALL SELECT 0
UNION ALL SELECT 0.00001
UNION ALL SELECT 100.01
UNION ALL SELECT 100.0102
UNION ALL SELECT 100.012300456
UNION ALL SELECT 1000
UNION ALL SELECT 123456789.87654321
UNION ALL SELECT -56500.333
)
SELECT
sample_number,
REGEXP_REPLACE(
FORMAT(sample_number, 999),
'(\.\d*[1-9])(0+$)|(\.0+$)',
''
) AS USA,
-- Replace \. with , for locales that use comma as decimal separator:
REGEXP_REPLACE(
FORMAT(sample_number, 999, 'de_DE'),
'(,\d*[1-9])(0+$)|(,0+$)',
''
) AS Germany
FROM sample_data;
sample_number | USA | Germany |
---|---|---|
NULL | NULL | NULL |
0.000000000 | 0 | 0 |
0.000010000 | 0.00001 | 0,00001 |
100.010000000 | 100.01 | 100,01 |
100.010200000 | 100.0102 | 100,0102 |
100.012300456 | 100.012300456 | 100,012300456 |
1000.000000000 | 1,000 | 1.000 |
123456789.876543210 | 123,456,789.87654321 | 123.456.789,87654321 |
-56500.333000000 | -56,500.333 | -56.500,333 |