Scala BigDecimal how to display more decimals? -



Scala BigDecimal how to display more decimals? -

first of all, aware there similar topic regarding partition of 1 3. however, code quite different , don't know how apply info given there situation. appreciate , help.

i made pi calculator in scala , @ end of code print result. however, want have much larger number of decimals , don't know how accomplish that. current output :

expected decimals: 34 3.141592653589793238462643383279500

here's code:

package thebrain object calculator { def main(args: array[string]) { var = 100 var j = 100 var lastvalueonfirstline = j+i var array = new array [bigdecimal] (0) var counter = i-1 (d <- j lastvalueonfirstline 1) { var almostpi = bigdecimal(0) var pi = bigdecimal(0) (b <- 0 d 1) { var partialanswer = (if (b%2 != 0) {-1} else {1} )/((bigdecimal(2)*bigdecimal(b))+bigdecimal(1)) almostpi = partialanswer + almostpi } pi = 4*almostpi array = array:+pi } (z <- counter 0 -1){ var array2 = new array [bigdecimal] (0) var length = array.length -2 (a <- 0 length 1){ var result = (array(a)+array(a+1))/2 array2 = array2:+result } array = array2 counter -= 1 } (element <- array) { println("expected decimals: " + element.precision) println(element) } } }

you have supply different java.math.mathcontext when create instances of bigdecimal. note effects precision of calculation, not how many decimals printed on output. quite different mere formatting of numbers.

scala> bigdecimal(1) / 3 res0: scala.math.bigdecimal = 0.3333333333333333333333333333333333 scala> res0.precision res1: int = 34 scala> bigdecimal(1, new java.math.mathcontext(50)) / 3 res2: scala.math.bigdecimal = 0.33333333333333333333333333333333333333333333333333 scala> res2.precision res3: int = 50

scala bigdecimal digits pi decimal

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -