Difference between revisions of "scala:Convert a String to a Number"

From Progzoo
Jump to: navigation, search
Line 1: Line 1:
 
{{topTenTips}}
 
{{topTenTips}}
 
<question height="100" width="150" className="Demo" rows="9" cols="40" lang='scala'>  
 
<question height="100" width="150" className="Demo" rows="9" cols="40" lang='scala'>  
<code>Integer.parseInt</code> is the static method that you need here.
+
<code>toInt</code> is the static method that you need here.
 
You have access to all of the java.lang packages such as http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
 
You have access to all of the java.lang packages such as http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
  
Line 11: Line 11:
 
   def main(args: Array[String]) {
 
   def main(args: Array[String]) {
 
     var s = "1234";
 
     var s = "1234";
     var i = s.toInt;
+
     var i = s toInt;
 
     println(i+1);
 
     println(i+1);
 
   }
 
   }

Revision as of 20:10, 16 February 2015

toInt is the static method that you need here. You have access to all of the java.lang packages such as http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)

See also:

  • toDouble
  • toLong


[Font] [Default] [Show] [Resize] [History] [Profile]