阅读更多
hive中的科学计数法如何转换回数字,搜了半天终于找到了一篇好文章,这里记录转换代码为自己日后参考。(转自:https://blog.csdn.net/zreodown/article/details/15506637)
case
--处理非科学计数法表示的字符串
when length(regexp_extract('字符串','([0-9]+\\.)([0-9]+)(E-*[0-9]+)',2))=0
then '字符串'
--处理整数
when length(regexp_extract('字符串','([0-9]+\\.)([0-9]+)(E[0-9]+)',2))<=cast(regexp_extract('字符串','(E)([0-9]+)',2) as int)
then rpad(regexp_replace(regexp_extract('字符串','([^E]+)',1),'\\.',''),cast(regexp_extract('字符串','(E)([0-9]+)',2) as int)+1,'0')
--处理小数
when length(regexp_extract('字符串','([0-9]+\\.)([0-9]+)(E[0-9]+)',2))>cast(regexp_extract('字符串','(E)([0-9]+)',2) as int)
then concat(substr(regexp_replace(regexp_extract('字符串','([^E]+)',1),'\\.',''),1,cast(regexp_extract('字符串','(E)([0-9]+)',2) as int)+1),'\.',
substr(regexp_replace(regexp_extract('字符串','([^E]+)',1),'\\.',''),cast(regexp_extract('字符串','(E)([0-9]+)',2) as int)+2))
--处理类似“3.4E-6”这种字符串
when '字符串' regexp 'E-'
then concat('0.',repeat('0',cast(regexp_extract('字符串','(E)(-)([0-9]+)',3) as int)-1),regexp_replace(regexp_extract('字符串','(.+)(E)',1),'\\.',''))
else '字符串'
end
例如要处理“1.5602200465E8”这样一个字符串:
select
case
when length(regexp_extract('1.5602200465E8','([0-9]+\\.)([0-9]+)(E-*[0-9]+)',2))=0 then '1.5602200465E8'
when length(regexp_extract('1.5602200465E8','([0-9]+\\.)([0-9]+)(E[0-9]+)',2))<=cast(regexp_extract('1.5602200465E8','(E)([0-9]+)',2) as int)
then rpad(regexp_replace(regexp_extract('1.5602200465E8','([^E]+)',1),'\\.',''),cast(regexp_extract('1.5602200465E8','(E)([0-9]+)',2) as int)+1,'0')
when length(regexp_extract('1.5602200465E8','([0-9]+\\.)([0-9]+)(E[0-9]+)',2))>cast(regexp_extract('1.5602200465E8','(E)([0-9]+)',2) as int)
then concat(substr(regexp_replace(regexp_extract('1.5602200465E8','([^E]+)',1),'\\.',''),1,cast(regexp_extract('1.5602200465E8','(E)([0-9]+)',2) as int)+1),'\.',
substr(regexp_replace(regexp_extract('1.5602200465E8','([^E]+)',1),'\\.',''),cast(regexp_extract('1.5602200465E8','(E)([0-9]+)',2) as int)+2))
when '1.5602200465E8' regexp 'E-'
then concat('0.',repeat('0',cast(regexp_extract('1.5602200465E8','(E)(-)([0-9]+)',3) as int)-1),regexp_replace(regexp_extract('1.5602200465E8','(.+)(E)',1),'\\.',''))
else '1.5602200465E8'
end
from dual;
结果:156022004.65