English 中文(简体)
我怎样才能展示大量数字?
原标题:How do I get Scala BigDecimal to display a large number of digits?
  • 时间:2011-11-22 06:57:41
  •  标签:
  • scala

如下:

  val num = BigDecimal(1.0)
  val den = BigDecimal(3.0)
  println((num/den)(MathContext.DECIMAL128))

我只想到

0.3333333333333333333333333333333333

少于128人

问题回答

缺省情况是MathContext。 DECIMAL128在所有计算中使用,例如,N/den的结果已经四舍五入到128处。 你们需要首先从所有价值观的角度来考虑,然后进行计算。

val mc = new MathContext(512)
val num = BigDecimal(1.0,mc)
val den = BigDecimal(3.0,mc)
println(num/den)

Don ttries and use MathContext. 联合国扫盲十年,除非你知道你的算术,否则不会产生无约束的代表性。 甚至在你试图印刷之前,它就会受到打击。

这项工作:

val mc = new java.math.MathContext(128)
val one_third = (BigDecimal(1, mc) / BigDecimal(3, mc)).toString
// 0. and a bunch of 3

one_third.filter(_ ==  3 ).size // returns 128

如果你使用512个数字,就会有512个数字。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签