本文整理汇总了Scala中java.text.DecimalFormat类的典型用法代码示例。如果您正苦于以下问题:Scala DecimalFormat类的具体用法?Scala DecimalFormat怎么用?Scala DecimalFormat使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DecimalFormat类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Conversions
//设置package包名称以及导入依赖的类
package com.wix.pay.tranzila.model
import java.text.DecimalFormat
object Conversions {
private val amountFormat = new DecimalFormat("0.00")
def toTranzilaAmount(amount: Double): String = {
amountFormat.format(amount)
}
def toTranzilaCurrency(currency: String): String = {
currency match {
case "ILS" => Currencies.ILS
case "USD" => Currencies.USD
case "GBP" => Currencies.GBP
case "HKD" => Currencies.HKD
case "JPY" => Currencies.JPY
case "EUR" => Currencies.EUR
case _ => throw new IllegalArgumentException(s"Tranzila doesn't support $currency currency")
}
}
def toTranzilaYearMonth(year: Int, month: Int): String = {
f"$month%02d${year % 100}%02d"
}
}
示例2: CmdType
//设置package包名称以及导入依赖的类
package libgcode
import java.text.{DecimalFormat,DecimalFormatSymbols}
import java.util.Locale
object CmdType extends Enumeration {
type CmdType = Value
val G, M, O, T, Empty = Value
}
object ParamType extends Enumeration {
type ParamType = Value
val A, B, C, D, E, F, H, I, J, K, L, P, Q, R, S, T, X, Y, Z = Value
}
import CmdType._
import ParamType.{T => PT, _}
// each line is a command
case class Command( ctype: CmdType, // Empty means it is a comment only
code: Seq[Int], // code X.Y corresponds to Seq(X, Y)
parameters: Seq[Param], // parameters
line: Option[Int], // line number (optional)
comment: Option[String]) { // trailing comment
def replaceComment(c: Option[String]) = {
if (c == comment) this
else Command(ctype, code, parameters, line, comment)
}
}
sealed abstract class Param
case class ParamT(ptype: ParamType) extends Param {
override def toString = ptype.toString
}
case class RealParam(ptype: ParamType, value: Double) extends Param {
assert(RealParam.is(ptype), ptype + " is not a real valued parameter")
override def toString = ptype.toString + RealParam.format(value)
}
case class IntParam(ptype: ParamType, value: Int) extends Param {
assert(IntParam.is(ptype), ptype + " is not an integer valued parameter")
override def toString = ptype.toString + value
}
object RealParam {
val types = Set(A, B, C, D, E, F, H, I, J, K, Q, R, X, Y, Z)
def is(t: ParamType) = types(t)
protected val df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH))
df.setMaximumFractionDigits(340)
def format(d: Double) = df.format(d)
}
object IntParam {
val types = Set(L, P, S, PT)
def is(t: ParamType) = types(t)
}
示例3: Formatter
//设置package包名称以及导入依赖的类
package com.github.twinra.fyber.window
import java.text.DecimalFormat
object Formatter {
val header: String = {
val space = " "
val columns = List("T" -> 10, "V" -> 7, "N" -> 2, "RS" -> 8, "MaxV" -> 7, "MinV" -> 7)
columns.map { case (k, v) => k + space * (v - k.length) }.mkString(space)
}
private val formatter = new DecimalFormat("#.#####")
private def format(v: Double) = formatter.format(v)
def format(rw: RollingWindow): String = {
"%d %-7s %-2d %-8s %-7s %-7s".format(
rw.lastTime,
format(rw.lastValue),
rw.measuresNumber,
format(rw.sum),
format(rw.min),
format(rw.max)
)
}
}
示例4: FileSizeConverter
//设置package包名称以及导入依赖的类
package DupeMaster
import java.text.DecimalFormat
object FileSizeConverter {
def apply(baseUnit: Int = 1000, unitChars: String = "kMGTPE", zero: String = "0") = new FileSizeConverter(baseUnit, unitChars, zero)
}
class FileSizeConverter(val baseUnit: Int, val unitChars: String, zero: String) {
val fileSizeRegex = """(?i)(\d+(?:\.\d+)?)?\s?((k|m|g|t|e)b?)?""".r
def stringToFileSize(s: String): Long = {
val (number, unit) = s match {
case fileSizeRegex(number, unit2, unit) => (number, unit)
case _ => ("0", null)
}
if (number == null) return 0
val factor = if (unit == null) 1 else math.pow(baseUnit, unitChars.toUpperCase.indexOf(unit.toUpperCase) + 1)
(math.max(0, number.toDouble * factor)).toLong
}
def fileSizeToString(bytes: Long): String = {
if (bytes < 1) return ""
if (bytes < baseUnit) return bytes.toString
val exp = (math.min(6, math.log(bytes) / math.log(baseUnit))).toInt
(new DecimalFormat("#.##")).format(bytes / Math.pow(baseUnit, exp)) + s" ${unitChars.charAt(exp - 1)}B"
}
}
示例5: SOSink
//设置package包名称以及导入依赖的类
package com.fyber.challenge.rollingregression.io
import java.text.DecimalFormat
import com.fyber.challenge.rollingregression.datatypes.{ResultContainer, Result}
object SOSink extends Sink {
private var headerHasPrinted: Boolean = false
override def put(container: ResultContainer): Unit =
if (headerHasPrinted) print(Formatter(container))
else {
println(Formatter.header)
print(Formatter(container))
headerHasPrinted = true
}
private object Formatter {
def apply(resultContainer: ResultContainer): String =
resultContainer.items.foldLeft("") ((acc, cr) => acc + format(cr))
private val decimalFormatter: DecimalFormat = new DecimalFormat("#.#####")
val header: String = {
val titles: String =
"%-10s %-8s %-3s %-12s %-7s %-9s" format ("T", "V", "N", "RS", "MaxV", "MinV")
val border: String = "-" * titles.length
titles + "\n" + border
}
private def roundup(value: Double): String = decimalFormatter format value
private def format(computationResult: Result): String =
"%d %-8s %-3d %-10s %9s %-9s".format(
computationResult.last.timestamp.value,
roundup(computationResult.last.priceRation.value),
computationResult.size.value,
roundup(computationResult.sum.value),
roundup(computationResult.max.value),
roundup(computationResult.min.value)
) + "\n"
}
}
示例6: richString
//设置package包名称以及导入依赖的类
package ro.dvulpe
import org.joda.time.format.DateTimeFormat
import java.util.Locale
import java.text.{NumberFormat, DecimalFormat}
package object ingparser {
implicit def richString(input: String) = new {
def asDateTime = DateTimeFormat.forPattern("dd MMMMM yyyy").withLocale(new Locale("RO")).parseDateTime(input)
def asDecimal = {
val format: DecimalFormat = NumberFormat.getInstance(new Locale("RO")).asInstanceOf[DecimalFormat]
format.setParseBigDecimal(true)
BigDecimal.apply(format.parse(input).asInstanceOf[java.math.BigDecimal])
}
}
}
示例7: TestDecoratorPattern
//设置package包名称以及导入依赖的类
package decorator
import java.text.DecimalFormat
object TestDecoratorPattern {
private val dFormat: DecimalFormat = new DecimalFormat("#.##")
def main(args: Array[String]) {
var pizza: Pizza = new SimplyVegPizza
pizza = new RomaTomatoes(pizza)
print(pizza)
pizza = new GreenOlives(pizza)
print(pizza)
pizza = new Spinach(pizza)
print(pizza)
pizza = new SimplyNonVegPizza
print(pizza)
pizza = new Meat(pizza)
print(pizza)
pizza = new Cheese(pizza)
print(pizza)
pizza = new Ham(pizza)
print(pizza)
}
private def print(pizza: Pizza) {
println("Desc: " + pizza.getDesc)
println("Price: " + dFormat.format(pizza.getPrice))
}
}
示例8: DecoratorSpec
//设置package包名称以及导入依赖的类
package test
import java.text.DecimalFormat
import decorator._
class DecoratorSpec extends BaseSpec {
private val dFormat: DecimalFormat = new DecimalFormat("#.##")
private val meatCheeseHamNonVegPrice = dFormat.format(403.09)
private val meatCheeseHamNonVegDesc = "SimplyNonVegPizza, Meat (14.25), Cheese (20.72), Ham (18.12)"
describe("A decorator") {
it("should decorate the given class") {
Given("a pizza")
val simplePizza: Pizza = new SimplyNonVegPizza()
When("the pizza is decorated")
val decoratedPizza: Pizza = new Ham(new Cheese(new Meat(simplePizza)))
Then("the pizza should have the expected toppings")
dFormat.format(decoratedPizza.getPrice) should be (meatCheeseHamNonVegPrice)
decoratedPizza.getDesc should be (meatCheeseHamNonVegDesc)
}
}
}
示例9: CheckoutItems
//设置package包名称以及导入依赖的类
import java.text.DecimalFormat
object CheckoutItems {
val ApplePrice = 0.6
val OrangePrice = 0.25
val Apple = "Apple"
val Orange = "Orange"
}
class CheckoutItems {
import CheckoutItems._
def checkoutItems(d: Double) = {
val decimalFormat = new DecimalFormat("0.00")
s"""£${decimalFormat.format(d)}"""
}
def scanItems(items: Map[String, Int]) = {
def calculateOffer(itemPrice: Double, itemCount: Int, offerAmount: Int, quantity: Int): Double = {
itemPrice * (((quantity / offerAmount) * itemCount) + ((quantity % offerAmount)))
}
items.foldLeft(0.0){ case (total, (item, quantity)) =>
val result = total + (item match {
case Apple =>
calculateOffer(ApplePrice, 1, 2, quantity)
case Orange =>
calculateOffer(OrangePrice, 2, 3, quantity)
case _ => throw new IllegalStateException("item not valid")
})
BigDecimal(result).setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble
}
}
}