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

From Progzoo
Jump to: navigation, search
Line 5: Line 5:
  
 
See also:
 
See also:
*<code>Integer.tryParse</code>
+
*<code>toDouble</code>
*<code>Double.parse</code>
+
*<code>toLong</code>
 
<prog>
 
<prog>
 
object Demo {
 
object Demo {
 
   def main(args: Array[String]) {
 
   def main(args: Array[String]) {
 
     var s = "1234";
 
     var s = "1234";
     var i = Integer.parseInt(s);
+
     var i = s.toInt;
 
     println(i+1);
 
     println(i+1);
 
   }
 
   }

Revision as of 20:10, 16 February 2015

Integer.parseInt 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]