基本类型转换String
object TypeDemo15 {
def main(args: Array[String]): Unit = {
var a:Int = 5
println((a+"").isInstanceOf[String])
}
}
String 类型转基本类型
object TypeDemo16 {
def main(args: Array[String]): Unit = {
var s2:String = "12"
var num1:Int = s2.toInt
var num2:Float = s2.toFloat
var num3:Double = s2.toDouble
var num4:Long = s2.toLong
println(s2.isInstanceOf[String])
println(num1.isInstanceOf[Int])
println(num2.isInstanceOf[Float])
println(num3.isInstanceOf[Double])
println(num4.isInstanceOf[Long])
}
}
注意事项
object TypeDemo14 {
def main(args: Array[String]): Unit = {
var s4 = "12.5"
println(s4.toInt) // 此处会报错
println(s4.toDouble.toInt)
}
}