mt8*_*t88 3 rounding apache-pig
How do you round a float or double down to the nth decimal place in pig. For example
f(3.999999, 1) = 3.9
f(3.42317, 2) = 3.42
f(1.03, 1) = 1.0
Run Code Online (Sandbox Code Playgroud)
I really only need to round to the 1st decimal place but thought i'd leave the question general. I saw the "pig round decimal to two places" question but the answer wasn't explained to the point where i could adapt it to this. Thanks
Turns out math solves this pretty easily. Just do:
FLOOR(column * 10^n) / 10^n
Run Code Online (Sandbox Code Playgroud)
PIG具有ROUND_TO。这将根据所需的小数点进行舍入。
B = FOREACH A GENERATE ROUND_TO(input,num_of_decimal_places);
Run Code Online (Sandbox Code Playgroud)
例:
输入文件:
3.999999
3.42317
1.03
Run Code Online (Sandbox Code Playgroud)
A =以(num:float)的形式加载'file'; B = FOREACH A GENERATE ROUND_TO(num,2); 转储B;
输出:
(4.0)
(3.42)
(1.03)
Run Code Online (Sandbox Code Playgroud)