字符转数字不使用str.toInt方法转为整型

object Str2Int {

  def main(args: Array[String]): Unit = {
    Str2Int("1234567") //true
  }

  def Str2Int(str: String): Unit = {
    implicit def double2Int(d:Double) = d.toInt
    var length = str.length
    val charArr = str.toCharArray
    var sum:Int = 0
    charArr.foreach(c=>{
      length -= 1
      sum = sum + (c - '0') * Math.pow( 10 ,length )
    })
    println(sum==str.toInt)
  }

}

你可能感兴趣的:(scala)