How to convert column values from string to decimal?
A Decimal has a precision and scale value, by default the precision is 10 and scale is 0.
The precision is the maximum number of digit in your number. In your case you have more than 10 digits so the number can't be cast to a 10 digits Decimal and you have null values.
To avoid that you need to specify a precision large enough to represent your numbers :
dframe.withColumn("c_number", dframe.col("c_a").cast(new DecimalType(38,0)))
Note that the precision can be up to 38
In scala-spark you can use the DecimalType for conversion:
import org.apache.spark.sql.types.DecimalType
val convertedDf = dframe.withColumn("c_number",trim(col("c_a")).cast(DecimalType(20,0)))
Try:
dframe.withColumn("c_number", dframe.col("c_a").cast("decimal(38,0)"))