Scala中所有的数值都是对象,包括Byte、Char、Double、Float、Int、Long和Shart。与被称为“非数值类型”的Unit和Boolean一样,这些数值类型都继承扩展自AnyVal特质(trait)
数据类型的精确范围值得查看:
scala> Short.MinValue
res45: Short = -32768
scala> Short.MaxValue
res46: Short = 32767
scala> Int.MinValue
res47: Int = -2147483648
scala> Int.MaxValue
res48: Int = 2147483647
复数和日期类型
复数–>Scala自带的math类库;
日期–>Joda Time
把一个字符串转换成一个Scala的数值类型–>使用String的to*方法
scala> val s = "100"
s: String = 100
scala> s.toInt
res53: Int = 100
scala> s.toDouble
res54: Double = 100.0
scala> s.toFloat
res55: Float = 100.0
scala> s.toLong
res56: Long = 100
scala> s.toShort
res57: Short = 100
scala> s.toByte
res58: Byte = 100
scala> val b = BigInt("1")
b: scala.math.BigInt = 1
scala> val b = BigDecimal("1")
b: scala.math.BigDecimal = 1
scala> Integer.parseInt("1", 2)
res60: Int = 1
scala> Integer.parseInt("10", 2)
res61: Int = 2
scala> Integer.parseInt("100", 2)
res62: Int = 4
scala> Integer.parseInt("1", 8)
res63: Int = 1
scala> Integer.parseInt("10", 8)
res64: Int = 8
把一个数值类型转换成另一中数值类型,不像Java中的强制类型转换,使用在所有数值类型上都可用的to*方法。
scala> 19.45.toInt
res68: Int = 19
scala> 19.toFloat
res69: Float = 19.0
scala> 19.toDouble
res70: Double = 19.0
scala> 19.toLong
res71: Long = 19
对一个数值型变量赋值时,Scala会自动把数值类型赋值给变量。定义一个数值类型变量时,可以重载其默认类型。
scala> val a = 1
a: Int = 1
scala> val a = 1d
a: Double = 1.0
scala> val a = 1f
a: Float = 1.0
scala> val a = 1000L
a: Long = 1000
也可以给变量增加类型声明
scala> val a = 0:Byte
a: Byte = 0
scala> val a = 0:Int
a: Int = 0
scala> val a = 0:Short
a: Short = 0
scala> val a = 0:Double
a: Double = 0.0
scala> val a = 0:Float
a: Float = 0.0
在指定变量的数据类型时,推荐使用如下格式:
scala> val a = 0:Float
a: Float = 0.0
scala> val a:Byte = 1
a: Byte = 1
类型归属—-java中的向上转型
scala> val s = "Dave"
s: String = Dave
scala> val p = s:Object
p: Object = Dave
scala> var a = 1
a: Int = 1
scala> a += 1
scala> a
res1: Int = 2
scala> print(a)
2
scala> a -= 1
scala> print(a)
1
浮点数值的比较
scala> val a = 0.3
a: Double = 0.3
scala> val b = 0.1 + 0.2
b: Double = 0.30000000000000004
Scala中scala.util.Random生成随机数。
scala> val r = scala.util.Random
r: util.Random.type = scala.util.Random$@139982de
scala> r.nextInt
res5: Int = -1286667537
scala> r.nextInt(100)
res6: Int = 60
区间
scala> val r = 1 to 10
r: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
// 设置步长
scala> val r = 1 to 10 by 2
r: scala.collection.immutable.Range = Range(1, 3, 5, 7, 9)
// for循环中使用区间
scala> for(i <- 1 to 5) println(i)
1
2
3
4
5
// 使用until来创建区间
scala> for(i <- 1 until 5) println(i)
1
2
3
4
将区间转换成其他序列
scala> val x = (1 to 10).toArray
x: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val x = (1 to 10).toList
x: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
//数值格式化
scala> val pi = scala.math.Pi
pi: Double = 3.141592653589793
scala> println(f"$pi%1.5f")
3.14159
// 格式化金额
scala> val formatter = java.text.NumberFormat.getCurrencyInstance
formatter: java.text.NumberFormat = java.text.DecimalFormat@774c1
scala> println(formatter.format(123.456789))
¥123.46
scala> println(formatter.format(12345.6789))
¥12,345.68