Hive Converting from Double to String Not in Scien

2019-05-08 01:14发布

I have a table with a column double type, and I am trying to convert that from double to string.

However, when I use the cast command, it "smartly" convert that into scientific notation. like below:

select 
    number, 
    cast(number as string), 
from ...

it looks like

number     c1_
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999902  9.999999902E9
9999999909  9.999999909E9

Can anyone show me how to avoid converting that into scientific and keep the raw text?

标签: hive
2条回答
淡お忘
2楼-- · 2019-05-08 01:39

Hive converts double to scientific representation while cast to string because Hive treats double itself in a same way. Therefore, problem is not with cast to string.

See below example:

 select 9999999902.0, cast(9999999902.0 as BIGINT), cast(cast(9999999902.0 as BIGINT)  as string) from ..

Output:

OK
9.999999902E9   9999999902
查看更多
放我归山
3楼-- · 2019-05-08 01:55

Hive supports good old printf() function so that you can control the output format explicitly - check Language Manual UDF under "String functions"

查看更多
登录 后发表回答