本文整理汇总了Scala中java.math.RoundingMode类的典型用法代码示例。如果您正苦于以下问题:Scala RoundingMode类的具体用法?Scala RoundingMode怎么用?Scala RoundingMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RoundingMode类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ProblemTwentySix
//设置package包名称以及导入依赖的类
package org.nason.euler.twenty
import java.math.{BigDecimal,MathContext,RoundingMode}
import org.nason.euler.EulerUtils
import org.nason.euler.NumberUtils
object ProblemTwentySix {
def main(args: Array[String])
{
println( solution )
}
val One = new BigDecimal(1.0)
val BIGDECIMAL = new MathContext( 2048, RoundingMode.HALF_UP )
def solution =
{
def reciprocal( v:Int ) = One.divide(new BigDecimal(v),BIGDECIMAL)
def bool2int( a:Boolean ) = if (a) 1; else 0
def autocorr( s:String ) =
{
for( d <- 0 until s.length-1 )
yield
( ( 0 until s.length-d )
.map( i => bool2int(s.charAt(i) equals s.charAt(i+d)) )
.reduceLeft(_+_) ) / (s.length-d).toDouble
}
def cycleLength( v:BigDecimal ) =
{
val seq = autocorr( v.toString.substring(2) )
if ( seq.length==0 )
0
else
{
val seq2 = seq.tail
if ( seq2.length==0 )
0
else
( NumberUtils argMax seq2 ) + 1
}
}
( for( d <- 2 until 1000 )
yield "%d: %d".format( d, cycleLength( reciprocal(d) ) ) )
.mkString("\n")
}
}
示例2: CalculateElapsedHours
//设置package包名称以及导入依赖的类
package name.mikulskibartosz.parkinglot.clock
import java.math.RoundingMode
import java.time.{Duration, ZonedDateTime}
import scala.util.Try
object CalculateElapsedHours {
def apply(currentTime: ZonedDateTime)(ticketCreationTime: ZonedDateTime): Try[Int] = Try{
if(ticketCreationTime.isAfter(currentTime))
throw new IllegalArgumentException(s"Ticket creation time is in the future. Current time: $currentTime, ticket creation time: $ticketCreationTime")
else if (ticketCreationTime.isEqual(currentTime))
1
else
BigDecimal(Duration.between(ticketCreationTime, currentTime).getSeconds).underlying()
.divide(BigDecimal(3600).underlying(), RoundingMode.CEILING)
.intValueExact()
}
}