(Decimal)小数点

Hive中的DECIMAL类型与Java的Big Decimal格式相同。它用于表示不变的任意精度。语法和示例如下:


DECIMAL(precision, scale)
decimal(10,0)

 

1、 基本类型

hive设置最大执行时间 hive decimal最大_类型转换

 

这里我们对DECIMAL类型做两点说明:
1)DECIMAL(9,8)代表最多9位数字,后8位是小数。此时也就是说,小数点前最多有1位数字,如果超过一位则会变成null。
2)如果不指定参数,那么默认是DECIMAL(10,0),即没有小数位,此时0.82会变成1。

 

这里我们通过一个SQL来进行说明:

创建表:

create table if not exists
datatype_test1(id int,col1 decimal,col2 decimal(9,8)) 
row format delimited fields terminated by ',';

我们从txt中读取数据:

load data local inpath '/Users/meituan_sxw/Downloads/test1.txt' into table datatype_test1;
#txt中的内容
1,0.82,83.2
2,1.06,9.22

接下来查看hive中的数据:

select * from datatype_test1;

hive设置最大执行时间 hive decimal最大_Hive_02

hive设置最大执行时间 hive decimal最大_Hive_03

hive设置最大执行时间 hive decimal最大_类型转换_04

hive设置最大执行时间 hive decimal最大_类型转换_05

hive设置最大执行时间 hive decimal最大_类型转换_06

 

类型转换:

The Types are organized in the following hierarchy (where the parent is a super type of all the children instances):

  • Type

Primitive Type

Number

DOUBLE

FLOAT

BIGINT

INT

SMALLINT

TINYINT

STRING

BOOLEAN

This type hierarchy defines how the types are implicitly converted in the query language. Implicit conversion is allowed for types from child to an ancestor. So when a query expression expects type1 and the data is of type2, type2 is implicitly converted to type1 if type1 is an ancestor of type2 in the type hierarchy. Note that the type hierarchy allows the implicit conversion of STRING to DOUBLE.

Explicit type conversion can be done using the cast operator as shown in the #Built In Functions section below.