Java Tutorial/Apache Common/NumberUtils
Содержание
NumberUtils.compare
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Compare two double values
System.out.println("(FIRST > SECOND) >>> " +
NumberUtils.rupare(2.11, 1.11));
System.out.println("(FIRST < SECOND) >>> " +
NumberUtils.rupare(1.11, 2.11));
System.out.println("(FIRST == SECOND) >>> " +
NumberUtils.rupare(1.11, 1.11));
}
}
(FIRST > SECOND) >>> 1 (FIRST < SECOND) >>> -1 (FIRST == SECOND) >>> 0
NumberUtils.createBigDecimal
import java.math.BigDecimal;
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Create a BigDecimal from a String
BigDecimal bDecimal = NumberUtils.createBigDecimal("123456789");
System.out.println(bDecimal);
}
}
123456789
NumberUtils.isDigits
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Check if a String contains only digits
System.out.println("Is Digits >>> " + NumberUtils.isDigits("123.123"));
}
}
123456789
NumberUtils.isNumber
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Check if a String is a valid number
System.out.println("Is Number >>> " + NumberUtils.isNumber("123.123"));
}
}
Is Number >>> true
NumberUtils.max
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Get MAX value from an array
System.out.println("MAX >>> " +
NumberUtils.max(new double[] { 3.33, 8.88, 1.11 }));
}
}
Is Number >>> true
NumberUtils.toInt
import org.apache.rumons.lang.math.NumberUtils;
public class MainClass {
public static void main(String[] args) {
//Convert String to int. If value invalid, return default value.
System.out.println("String to Int >>> " +
NumberUtils.toInt("ABCD", 77));
}
}
String to Int >>> 77