coder25
coder25

Reputation: 2393

print float without scientific notation in scala

I want to convert float to 14 significant digits

val s = "1200000000".toFloat

Output-1.2E9

I tried below but does not work

f"$s%1.0f" but this doesnot work with all values I want a method which takes in string and return a float .The method can support upto 15 significant digits

Upvotes: 0

Views: 3541

Answers (2)

mkUltra
mkUltra

Reputation: 3068

You can use toPlainString method of java.math.BigDecimal:

val floatValue = "1200000000".toFloat

new java.math.BigDecimal(floatValue).toPlainString 

res0: String = 1200000000

Upvotes: 3

SCouto
SCouto

Reputation: 7928

What do you want exactly?

If you want to print a number with a given format you can use this:

println(f"$myNumber%1.14f")

The output will be:

1200000000.00000000000000

You can get more information here

Upvotes: 1

Related Questions