How to truncate a double to four digits in hive without rounding?

7.2k Views Asked by At

I need to truncate a number to 4 digits after the dot, without rounding it. I tried to use format_number(num,4) but it does do rounding. I need to truncate it without rounding, just to throw away the other digits. any ideas?

1

There are 1 best solutions below

0
On BEST ANSWER

There would be 2 way to do it since all hive supplied function would end up rounding.

  • You Multiply double by 10000 -> Cast/format to INT to drop digits -> Divide by 10000
  • You can write your own UDF to do it. Writing UDFs is very easy. Here is link to get started

http://lets-do-something-big.blogspot.com/2014/02/hive-udf-example.html