java.math.RoundingMode Scala Examples
The following examples show how to use java.math.RoundingMode.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: Amount.scala From scala-stellar-sdk with Apache License 2.0 | 5 votes |
package stellar.sdk.model import java.math.{MathContext, RoundingMode} import java.util.Locale import cats.data.State import stellar.sdk.model.xdr.{Decode, Encodable, Encode} import scala.util.Try sealed trait Amount extends Encodable { val units: Long val asset: Asset def toDisplayUnits: String = "%.7f".formatLocal(Locale.ROOT, BigDecimal(units) / Amount.toIntegralFactor) def encode: LazyList[Byte] = asset.encode ++ Encode.long(units) } case class NativeAmount(units: Long) extends Amount { override val asset: Asset = NativeAsset override def toString: String = s"$toDisplayUnits XLM" } case class IssuedAmount(units: Long, asset: NonNativeAsset) extends Amount { override def toString: String = s"$toDisplayUnits $asset" } object Amount extends Decode { private val decimalPlaces = 7 private val toIntegralFactor = BigDecimal(math.pow(10, decimalPlaces)) def toBaseUnits(d: Double): Try[Long] = toBaseUnits(BigDecimal(d)) def toBaseUnits(s: String): Try[Long] = Try(BigDecimal(s)).flatMap(toBaseUnits) def toBaseUnits(bd: BigDecimal): Try[Long] = Try { (bd * toIntegralFactor.round(new MathContext(0, RoundingMode.DOWN))).toLongExact } def apply(units: Long, asset: Asset): Amount = { asset match { case NativeAsset => NativeAmount(units) case a: NonNativeAsset => IssuedAmount(units, a) } } def lumens(units: Double): NativeAmount = toBaseUnits(units).map(NativeAmount).getOrElse( throw new IllegalArgumentException(s"Too many digits in fractional portion of $units. Limit is $decimalPlaces") ) def decode: State[Seq[Byte], Amount] = for { asset <- Asset.decode units <- long } yield apply(units, asset) } object IssuedAmount { def decode: State[Seq[Byte], IssuedAmount] = Amount.decode.map(x => x.asInstanceOf[IssuedAmount]) }
Example 2
Source File: DoubleUtils.scala From seahorse with Apache License 2.0 | 5 votes |
package ai.deepsense.commons.utils import java.math.{MathContext, RoundingMode} object DoubleUtils { private val significantFigures = 6 private val mathContext = new MathContext(significantFigures, RoundingMode.HALF_UP) def double2String(d: Double): String = { if (d.isNaN || d.isInfinity) { d.toString } else { val decimal = BigDecimal(d) .round(mathContext) .toString() if (decimal.contains("E")) { decimal.replaceAll("\\.?0*E", "e") } else if (decimal.contains(".")) { decimal.replaceAll("\\.?0*$", "") } else { decimal } } } }
Example 3
Source File: ThreadLocalNumberFormat.scala From perfolation with MIT License | 5 votes |
package perfolation import java.math.RoundingMode import java.text.NumberFormat import java.util.Currency object ThreadLocalNumberFormat { // Make sure the platform is initialized Platform private val threadLocalNumberFormat = new ThreadLocal[NumberFormat]{ override protected def initialValue(): NumberFormat = NumberFormat.getInstance() } protected[perfolation] def apply(i: Int, f: Int, maxI: Int, maxF: Int, g: Boolean, c: Option[Currency], rm: RoundingMode): NumberFormat = { val nf = threadLocalNumberFormat.get() nf.setGroupingUsed(g) c.foreach(nf.setCurrency) nf.setMaximumFractionDigits(if (maxF == -1) f else maxF) nf.setMinimumFractionDigits(f) nf.setMaximumIntegerDigits(if (maxI == -1) i else maxI) nf.setMinimumIntegerDigits(i) nf.setParseIntegerOnly(maxF == 0) nf.setRoundingMode(rm) nf } }
Example 4
Source File: DataGens.scala From spark-vector with Apache License 2.0 | 5 votes |
package com.actian.spark_vector import java.math.BigDecimal import java.{ sql => jsql } import java.util.Calendar import scala.collection.Seq import scala.util.Try import org.apache.spark.sql.Row import org.apache.spark.sql.types._ import org.scalacheck.Gen import com.actian.spark_vector.colbuffer.util.MillisecondsInDay import java.math.RoundingMode object DataGens { import com.actian.spark_vector.DataTypeGens._ import org.scalacheck.Arbitrary._ import org.scalacheck.Gen._ import scala.collection.JavaConverters._ val DefaultMaxRows = 500 val booleanGen: Gen[Boolean] = arbitrary[Boolean] val byteGen: Gen[Byte] = arbitrary[Byte] val shortGen: Gen[Short] = arbitrary[Short] val intGen: Gen[Int] = arbitrary[Int] val longGen: Gen[Long] = arbitrary[Long] // FIXME allow arbitrary doubles (and filter externally for vector tests) val floatGen: Gen[Float] = arbitrary[Float].map(f => if (f.abs > 1e-38) f else 0.0f) // FIXME allow arbitrary doubles (and filter externally for vector tests) val doubleGen: Gen[Double] = for { neg <- arbitrary[Boolean] digits <- listOfN(12, choose(0, 9)) } yield s"${if (neg) "-" else ""}1.${digits.mkString("")}".toDouble val decimalGen: Gen[BigDecimal] = arbitrary[scala.BigDecimal].retryUntil(bd => bd.scale <= 12 && bd.scale >= 0 && bd.precision <= 26 && Try { new BigDecimal(bd.toString) }.isSuccess).map(bd => new BigDecimal(bd.toString)) private val dateValueGen: Gen[Long] = choose(-3600L * 1000 * 24 * 100000L, 3600L * 1000 * 24 * 100000L) // @note normalize getTime so that we don't have diffs more than 1 day in between our {JDBC,Spark}results val dateGen: Gen[jsql.Date] = dateValueGen.map(d => new jsql.Date(d / MillisecondsInDay * MillisecondsInDay)) val timestampGen: Gen[jsql.Timestamp] = for (ms <- dateValueGen) yield new jsql.Timestamp(ms) // FIXME allow empty strings (and filter externally for vector tests) // @note we do not allow invalid UTF8 chars to be generated (from D800 to DFFF incl) val stringGen: Gen[String] = listOfN(choose(1, 512).sample.getOrElse(1), arbitrary[Char]).map(_.mkString).map( s => s.filter(c => Character.isDefined(c) && c != '\u0000' && (c < '\uD800' || c > '\uDFFF')) ) def valueGen(dataType: DataType): Gen[Any] = dataType match { case BooleanType => booleanGen case ByteType => byteGen case ShortType => shortGen case IntegerType => intGen case LongType => longGen case FloatType => floatGen case DoubleType => doubleGen case TimestampType => timestampGen case DateType => dateGen case StringType => stringGen case _: DecimalType => decimalGen case _ => throw new Exception("Invalid data type.") } def nullableValueGen(field: StructField): Gen[Any] = { val gen = valueGen(field.dataType) if (field.nullable) frequency(1 -> gen, 10 -> const(null)) else gen } def rowGen(schema: StructType): Gen[Row] = sequence(schema.fields.map(f => nullableValueGen(f))).map(l => Row.fromSeq(l.asScala)) // TODO Huh? Why ju.ArrayList?!? def dataGenFor(schema: StructType, maxRows: Int): Gen[Seq[Row]] = for { numRows <- choose(1, maxRows) rows <- listOfN(numRows, rowGen(schema)) } yield rows case class TypedData(dataType: StructType, data: Seq[Row]) val dataGen: Gen[TypedData] = for { schema <- schemaGen data <- dataGenFor(schema, DefaultMaxRows) } yield TypedData(schema, data) val allDataGen: Gen[TypedData] = for { schema <- allTypesSchemaGen data <- dataGenFor(schema, DefaultMaxRows) } yield TypedData(schema, data) }
Example 5
Source File: DoubleUtils.scala From seahorse-workflow-executor with Apache License 2.0 | 5 votes |
package io.deepsense.commons.utils import java.math.{MathContext, RoundingMode} object DoubleUtils { private val significantFigures = 6 private val mathContext = new MathContext(significantFigures, RoundingMode.HALF_UP) def double2String(d: Double): String = { if (d.isNaN || d.isInfinity) { d.toString } else { val decimal = BigDecimal(d) .round(mathContext) .toString() if (decimal.contains("E")) { decimal.replaceAll("\\.?0*E", "e") } else if (decimal.contains(".")) { decimal.replaceAll("\\.?0*$", "") } else { decimal } } } }