本文整理汇总了Scala中scala.math.BigDecimal.RoundingMode类的典型用法代码示例。如果您正苦于以下问题:Scala RoundingMode类的具体用法?Scala RoundingMode怎么用?Scala RoundingMode使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RoundingMode类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: OrderProcessor
//设置package包名称以及导入依赖的类
package com.example
import java.util.UUID
import scaldi.Injector
import akka.actor.{Actor, ActorRef, PoisonPill}
import scaldi.akka.AkkaInjectable
import scala.math.BigDecimal.RoundingMode
class OrderProcessor(implicit inj: Injector) extends Actor with AkkaInjectable {
import Messages._
val priceCalculator = injectActorRef [PriceCalculator]
def receive = idle
val idle: Receive = {
case orderInfo @ ProcessOrder(user: User, itemId: Long, netAmount: Int) =>
println(s"Processing order for user $user.")
priceCalculator ! CalculatePrice(netAmount)
context become workingHard(orderInfo, sender)
}
def workingHard(orderInfo: ProcessOrder, reportTo: ActorRef): Receive = {
case CancelProcessing =>
reportTo ! OrderProcessingFailed("Canceled..")
self ! PoisonPill
case GrossPriceCalculated(_, grossPrice) =>
println("Processing order.....")
reportTo ! OrderProcessed(UUID.randomUUID().toString, grossPrice)
self ! PoisonPill
}
}
class PriceCalculator extends Actor {
import Messages._
def receive = {
case CalculatePrice(netAmount) =>
val grossCent = (netAmount * BigDecimal("1.19")).setScale(0, RoundingMode.HALF_UP).toIntExact
sender ! GrossPriceCalculated(netAmount, grossCent)
}
}
示例2: Money
//设置package包名称以及导入依赖的类
package com.outr.stripe
import scala.math.BigDecimal.RoundingMode
class Money private(val value: BigDecimal) {
override def equals(obj: scala.Any): Boolean = obj match {
case m: Money => m.value == value
case b: BigDecimal => b == value
case _ => false
}
def pennies: Long = (value * 100.0).toLongExact
override def toString: String = f"$$$value%1.2f"
}
object Money {
def apply(d: BigDecimal): Money = new Money(d.setScale(2, RoundingMode.HALF_EVEN))
def apply(d: Double): Money = apply(BigDecimal(d))
def apply(pennies: Long): Money = apply(BigDecimal(pennies) / 100.0)
def apply(s: String): Money = apply(BigDecimal(s))
}
示例3: StatisticsCalculator
//设置package包名称以及导入依赖的类
package fi.kajstrom.efpscala.E36
import scala.math.BigDecimal.RoundingMode
class StatisticsCalculator {
def calculate(numbers: Vector[Int]) : Statistics = {
var minimum = 0
var maximum = 0
var average = 0.0
var stdDeviation = 0.0
//Exercise constraint is to use loops. I'd prefer collection methods otherwise.
numbers.foreach((number: Int) => {
if (number < minimum) {
minimum = number
} else if (minimum == 0) {
minimum = number
}
if (number > maximum) {
maximum = number
}
average += number
})
average = average.toDouble / numbers.length
//Std deviation
numbers.foreach((number: Int) => {
var diffFromMean = number - average
diffFromMean = scala.math.abs(diffFromMean)
diffFromMean = diffFromMean * diffFromMean
stdDeviation += diffFromMean
})
stdDeviation = stdDeviation / numbers.length
stdDeviation = scala.math.sqrt(stdDeviation)
stdDeviation = BigDecimal(stdDeviation).setScale(2, RoundingMode.HALF_UP).toDouble
Statistics(
average,
minimum,
maximum,
stdDeviation
)
}
}
case class Statistics(average: Double, minimum: Int, maximum: Int, standardDeviation: Double) {}
示例4: StandardDeviationCalculator
//设置package包名称以及导入依赖的类
package fi.kajstrom.efpscala.E36Actor
import akka.actor.Actor
import scala.math.BigDecimal.RoundingMode
class StandardDeviationCalculator extends Actor{
def receive = {
case calculate: Calculate => {
context.parent ! StandardDeviationCalculated(calculate.requestId, standardDeviationOf(calculate.numbers))
}
}
def standardDeviationOf(numbers: Vector[Int]): Double = {
var stdDeviation = 0.0
val average = numbers.sum / numbers.length
//Std deviation
numbers.foreach((number: Int) => {
var diffFromMean = number - average
diffFromMean = scala.math.abs(diffFromMean)
diffFromMean = diffFromMean * diffFromMean
stdDeviation += diffFromMean
})
stdDeviation = stdDeviation / numbers.length
stdDeviation = scala.math.sqrt(stdDeviation)
BigDecimal(stdDeviation).setScale(2, RoundingMode.HALF_UP).toDouble
}
}
case class StandardDeviationCalculated(reguestId: Int, standardDeviation: Double)
示例5: render
//设置package包名称以及导入依赖的类
package org.aguo.civsim.model.domain
import org.aguo.civsim.model._
import scala.math.BigDecimal.RoundingMode
import Console._
sealed trait Job {
def render: String = s"$YELLOW$toString$RESET"
def examine(world: World): String
}
object Job {
def unapply(string: String): Option[Job] = string match {
case "farmer" => Some(Farmer)
case "builder" => Some(Builder)
case _ => None
}
}
sealed trait FoodJob extends Job {
def foodProduction(world: World): BigDecimal
override def examine(world: World): String = {
s"$render produces ${GREEN}${foodProduction(world).setScale(2, RoundingMode.HALF_UP)} food${RESET}."
}
}
case object Farmer extends FoodJob {
override def foodProduction(world: World): BigDecimal = {
1.2
}
}
trait ProductionJob extends Job {
def production(world: World): BigDecimal
override def examine(world: World): String = {
s"$render has ${RED}${production(world).setScale(2, RoundingMode.HALF_UP)} production${RESET}."
}
}
case object Builder extends ProductionJob {
override def production(world: World): BigDecimal = {
1.0
}
}
示例6: PlacesControllerSingleton
//设置package包名称以及导入依赖的类
package places
import clients.{GooglePlacesClient, GooglePlacesClientSingleton}
import models.{AddressResult, DistanceResult}
import scala.concurrent.{ExecutionContext, Future}
import scala.math.BigDecimal.RoundingMode
object PlacesControllerSingleton extends PlacesController(GooglePlacesClientSingleton)
class PlacesController(placesClient: GooglePlacesClient) {
def getDistanceResult(origin: String, destination: String)(implicit ec: ExecutionContext): Future[DistanceResult] = {
val eventualOrigin = placesClient.getAddress(origin)
val eventualDestination = placesClient.getAddress(destination)
val eventualDistanceTo = placesClient.getDistance(origin, destination)
val eventualDistanceFrom = placesClient.getDistance(destination, origin)
val metersInMiles: BigDecimal = 1609.34
for {
distanceTo <- eventualDistanceTo
distanceFrom <- eventualDistanceFrom
distance = BigDecimal((distanceTo.inMeters + distanceFrom.inMeters) / 2.0)
origin <- eventualOrigin
destination <- eventualDestination
miles: BigDecimal = (distance / metersInMiles).setScale(2, RoundingMode.FLOOR)
result = DistanceResult(distance.toLong, miles, Some(origin), Some(destination))
} yield result
}
def getAddress(address: String)(implicit ec: ExecutionContext): Future[AddressResult] = {
placesClient.getAddress(address)
}
}
示例7: TransactionHelpers
//设置package包名称以及导入依赖的类
package utils
import models.{Months, Transaction, Transactions}
import scala.collection.immutable.ListMap
import scala.math.BigDecimal.RoundingMode
object TransactionHelpers {
def groupTransactionsByMonth(transactions: Transactions): Seq[((Int, Int), Seq[Transaction])] = {
val transactionsWithoutTopUps = transactions.transactions.filterNot(_.isLoad)
val groupedAndSortedByMonth = transactionsWithoutTopUps.groupBy(a => (a.created.getMonthOfYear, a.created.getYear)).toSeq.sortWith(_._1._1 > _._1._1).sortWith(_._1._2 > _._1._2)
groupedAndSortedByMonth
}
def calculateRoundupsByMonth(accountId: String, transactions: Transactions)(accessToken: String): Map[String, BigDecimal] = {
val byMonth = ListMap(transactions.transactions.filterNot(_.isLoad).groupBy(a => (a.created.getMonthOfYear, a.created.getYear)).toSeq.sortWith(_._1._1 > _._1._1).sortWith(_._1._2 > _._1._2): _*)
byMonth.map {
month =>
val roundUps = month._2.map { eachMonth =>
val roundedValue = (eachMonth.amount / 100 setScale(0, RoundingMode.UP)).abs
val rawValue = (eachMonth.amount / 100).abs
val roundUp = roundedValue - rawValue
if (roundUp.equals(BigDecimal(0))) BigDecimal(1) // £1 roundups when transaction amount is whole number
else roundUp
}.foldLeft(BigDecimal(0))(_ + _)
val formData = Map(
"account_id" -> Seq(accountId),
"type" -> Seq("basic"),
"url" -> Seq(""),
"params[title]" -> Seq(s"Round Up for ${Months(month._1._1)} ${month._1._2} - £$roundUps"),
"params[body]" -> Seq(s"Round ups for ${Months(month._1._1)} ${month._1._2} - £$roundUps"),
"params[image_url]" -> Seq("https://scontent-lht6-1.xx.fbcdn.net/v/t1.0-9/15871922_10212040156182063_1392533991348799017_n.jpg?oh=4669484d186b91d9b07911255a8d09d3&oe=5940244F")
)
//ws.url("https://api.monzo.com/feed").withHeaders(("Authorization", s"Bearer $accessToken")).post(formData)
(s"${Months(month._1._1)} ${month._1._2}", roundUps)
}
}
}
示例8: MathUtil
//设置package包名称以及导入依赖的类
package davidkellis.seven
import scala.math.BigDecimal.RoundingMode
object MathUtil {
def ceil(decimal: Double): Double = Math.ceil(decimal)
def ceil(decimal: BigDecimal): BigDecimal = decimal.setScale(0, RoundingMode.CEILING)
def intCeil(decimal: Double): Int = ceil(decimal).toInt
def intCeil(decimal: BigDecimal): Int = ceil(decimal).toInt
def floor(decimal: Double): Double = Math.floor(decimal)
def floor(decimal: BigDecimal): BigDecimal = decimal.setScale(0, RoundingMode.FLOOR)
def intFloor(decimal: Double): Int = floor(decimal).toInt
def intFloor(decimal: BigDecimal): Int = floor(decimal).toInt
def round(decimal: Double): Double = Math.round(decimal)
// see section on HALF_UP on http://docs.oracle.com/javase/6/docs/api/index.html?java/math/RoundingMode.html
// -> on HALF_UP: "this is the rounding mode commonly taught at school"
def round(decimal: BigDecimal): BigDecimal = decimal.setScale(0, RoundingMode.HALF_UP)
def intRound(decimal: Double): Int = round(decimal).toInt
def intRound(decimal: BigDecimal): Int = round(decimal).toInt
}