1

If I'm using myDF.write.csv("wherever"), how can I set the numeric format for stored data? EG, if I do:

val t = spark.sql("SELECT cast(1000000000000 as double) as aNum")
t.write.csv("WXYZ")

and then review WXYZ, I will find I have 1.0E12. How could I change this for all doubles such that I get 1000000000000.00?

2 Answers 2

1

The way I've handled this issue is by casting the number to a string

val t = spark.sql("SELECT cast(1000000000000 as string) as aNum")
t.write.csv("WXYZ")
t.show()

And the output is

+-------------+
|         aNum|
+-------------+
|1000000000000|
+-------------+

:) I hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I was hoping to do it for a class of items, rather than for each item.
what do you mean a "class of terms"? like a column?
0

if data comes from hive there is a hive udf printf u can use:

select printf('%.2f', col) from foobar

planB:

dataset.map( col => s"$col%.2f")

take care of planB, there may be extra cost based on your data source

btw, sometimes it is likely just a problem of display in excel, just check the csv with text editor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.