Oviic
Oviic

Reputation: 45

How to convert double with scientific format to String Using spark Java API?

I'm new in spark Java API. I want to transform double with scientific format example: 1.7E7---->17000000,00.

MyDataSet is:

+---------+------------+
|  account|    amount  |
+---------+------------+
| c1      |      1.7E7 |
| c2      |      1.5E8 |
| c3      |      142.0 |

I want to transform my dataset to something like this.

    +---------+----------------------+
    |  account|    amount            |
    +---------+----------------------+
    | c1      |      17000000,00     |
    | c2      |      1500000000,00   |
    | c3      |      142,00          |

Can someOne guide me with an expression in spark Java to resolve this. Thanks in advance.

Upvotes: 0

Views: 3608

Answers (1)

Danila Zharenkov
Danila Zharenkov

Reputation: 1863

I think you can do it like this. Don't forget import spark.sql.functions

Dataset<Row> myDataset = ....
myDataset = myDataset.withColumn("newAmount", col("amount").cast(DataTypes.DoubleType))
    .drop(col("amount"))
    .withColumnRenamed("newAmount","amount")
    .toDF();

Upvotes: 3

Related Questions