tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Tools and Libs > Commons Lang3 > Numbers > To Integer

To Integer 

Apache Commons Lang 3.0 is a java library with lot of utilities and reusable components. This requires the library commons-lang3-3.0.1.jar to be in classpath. The following example shows using NumberUtils.toInt() API. It converts the string to int. If the conversion fails, it returns ZERO.

File Name  :  
com/bethecoder/tutorials/commons_lang/tests/numbers/ToIntTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.commons_lang.tests.numbers;

import org.apache.commons.lang3.math.NumberUtils;

public class ToIntTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    //NumberUtils.toInt(String str)
    System.out.println(NumberUtils.toInt(null));
    System.out.println(NumberUtils.toInt(""));
    System.out.println(NumberUtils.toInt("123"));

    //NumberUtils.toInt(String str, int defaultValue)
    System.out.println(NumberUtils.toInt(null, 6));
    System.out.println(NumberUtils.toInt(""6));
    System.out.println(NumberUtils.toInt("123"6));
  }

}
   

It gives the following output,
0
0
123

6
6
123


                    Print

   



Books Recommendation

 
 
bl  br