Char: 16位无符号Unicode
Byte: 8位有符号整数
Int: 32有符号整数
Long: 64有符号整数
Float : 32位单精度浮点型
Double:64双精度浮点
scala> Short.MinValue
Short.MinValue
res3: Short = -32768
scala> Long.MaxValue
Long.MaxValue
res4: Long = 9223372036854775807
其他更丰富的数值类型来自math库
日期数据:Joda time
2.1 字符串转数值
scala> "100".toInt
"100".toInt
res7: Int = 100
scala> "100".toFloat
"100".toFloat
res8: Float = 100.0
scala> "foo".toInt
"foo".toInt
java.lang.NumberFormatException: For input string: "foo"
scala> BigDecimal("3.1415926")
BigDecimal("3.1415926")
- 处理基数和根
import java.lang.Integer
// 2进制
scala> Integer.parseInt("100", 2)
Integer.parseInt("100", 2)
res12: Int = 4
隐式转换实现
scala> implicit class String2Int(s:String){
| def toInt(radix:Int) = Integer.parseInt(s, radix)
| }
scala> "100".toInt(2)
"100".toInt(2)
res13: Int = 4
每次运行一个object必须定义main函数,另外一种方法参考http://spreadscala.iteye.com/blog/667534
object allrepl {
def main(args:Array[String]):Unit={
val x = "1000"
println(x.toInt(2).getOrElse(0)) // out:8
println("a".toInt(2).getOrElse(0)) //out:0
}
implicit class String2Int(s:String){
// import java.lang.Integer
def toInt(radix:Int):Option[Int] = {
try {
Some(Integer.parseInt(s, radix))
} catch {
case e: NumberFormatException => None
}
}
}
}
对不同的数值类型之间相互转化的时候为了表面错误 判断方法:1.isValidByte
2.3 重载数据类型
在一个类中定义变量可以用占位符
cala> val a = 1
a: Int = 1
scala> val a:Long = 1
a: Long = 1
scala> val a = 1:Long
a: Long = 1
scala> val a = 1l
a: Long = 1
2.4 += 方法(不是操作符)
scala> var a = 1
a: Int = 1
scala> a += 1
scala> a
res5: Int = 2
+= -= *= /=
2.5 不同精度的比较运算设定误差范围
2.6 处理大数
scala> BigInt(123456789)
res0: scala.math.BigInt = 123456789
scala> BigDecimal(123456789)
res4: scala.math.BigDecimal = 123456789
scala> Double.PositiveInfinity
res2: Double = Infinity
scala> Double.NegativeInfinity
res3: Double = -Infinity
2.7 Random
scala> val r = scala.util.Random
r: util.Random.type = scala.util.Random$@759d26fb
scala> r.nextInt
res6: Int = 192058607
// 0-100
scala> r.nextInt(100)
res14: Int = 24
scala> r.nextDouble // 0-1
res8: Double = 0.5660426827712413
scala> r.nextFloat //0-1
res9: Float = 0.98220724
scala> r.setSeed(1000) //可在定义r的时候设定
2.8 数值区间
scala> 1 to 10
res15: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> 1 to 10 by 2
res16: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
scala> 1 until 10
res17: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> 1 until 10 toArray
warning: there was one feature warning; re-run with -feature for details
res18: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
2.9 格式化数值金额
scala> import scala.math.Pi
import scala.math.Pi
scala> f"$Pi%1.3f"
res20: String = 3.142
scala> "%1.3f".format(Pi)
res21: String = 3.142
scala> import java.text.NumberFormat.getIntegerInstance
import java.text.NumberFormat.getIntegerInstance
scala> getIntegerInstance.format(1000000000)
res22: String = 1,000,000,000
scala> import java.text.NumberFormat.get
getAvailableLocales getInstance getNumberInstance
getCurrencyInstance getIntegerInstance getPercentInstance
- 货币
import java.util.{Currency, Locale}
val loc = new Locale("de", "DE")
val de = Currency.getInstance(loc)
val formater = java.text.NumberFormat.getCurrencyInstance
println(formater.format(10)) // 默认美元
formater.setCurrency(de)
println(formater.format(10)) // 欧元
网友评论