Skip to content
Advertisement

How can I turn off rounding in Spark?

I have a dataframe and I’m doing this:

JavaScript

I want to get just the first four numbers after the dot, without rounding.

When I cast to DecimalType, with .cast(DataTypes.createDecimalType(20,4) or even with round function, this number is rounded to 0.4220.

The only way that I found without rounding is applying the function format_number(), but this function gives me a string, and when I cast this string to DecimalType(20,4), the framework rounds the number again to 0.4220.

I need to convert this number to DecimalType(20,4) without rounding, and I expect to see 0.4219.

Advertisement

Answer

If you have numbers with more than 1 digit before the decimal point, the substr is not adapt. Instead, you can use a regex to always extract the first 4 decimal digits (if present).
You can do this using regexp_extract

JavaScript

Example

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement