When I use doubles for computation in Python, formatting the output of those doubles gives back strings that look like this:
>>> v = 1 / 10.0
>>> v
0.1
>>> "%.25f" % v
'0.1000000000000000055511151'
However, when I do something similar with Scala, I get zeroes at the end:
scala> val v = 1 / 10.0
v: Double = 0.1
scala> "%.25f" format v
res0: String = 0.1000000000000000000000000
I would like to use Scala to output double-precision decimals with the same precision/rounding/whatever as Python, so that I can produce identical output from both the Scala and Python code. Does anyone know a way to do this?
Thanks!
Edit
I've done some experimentation, and this Java program seems to produce identical output to the Python code:
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
double v1 = 1.0;
double v2 = 10.0;
double v3 = v1 / v2;
BigDecimal bd1 = new BigDecimal(v3);
System.out.println(String.format("%.25f", bd1));
}
}
Created a similar program in Scala, but it doesn't work:
object Main extends App {
val v1: Double = 1.0d
val v2: Double = 10.0d
val v3: Double = v1 / v2
val bd1: BigDecimal = BigDecimal(v3)
println("%.25f" format bd1)
}
Perhaps the best option for someone in my circumstance would be to include some plain-Jane Java code in the Scala project for the purposes of formatting. Will mark the question as answered.