当前位置:网站首页>[Commons lang3 topic] 004- numberutils topic
[Commons lang3 topic] 004- numberutils topic
2022-07-29 00:58:00 【Zibo Zibo】
【commons-lang3 project 】004- NumberUtils project
List of articles
- 【commons-lang3 project 】004- NumberUtils project
- 〇、 Get ready
- One 、 Common constants
- Two 、 Compare the size
- 3、 ... and 、 String rotation BigInteger and BigInteger
- Four 、 String to number wrapper class
- 5、 ... and 、 Determine whether the string is related to the number
- 6、 ... and 、 Take the extreme value
- 7、 ... and 、 String to basic data type
- 8、 ... and 、 Complete code
〇、 Get ready
1、NumberUtils The main role
Provide and generate various Digital operation Method .
2、 Introduce dependencies
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
One 、 Common constants
1、Long value
// 1、Long value
System.out.println(NumberUtils.LONG_ZERO); // 0
System.out.println(NumberUtils.LONG_ONE); // 1
System.out.println(NumberUtils.LONG_MINUS_ONE); // -1
System.out.println(NumberUtils.LONG_INT_MIN_VALUE); // -2147483648
System.out.println(NumberUtils.LONG_INT_MAX_VALUE); // 2147483647
2、Integer value
// 2、Integer value
System.out.println(NumberUtils.INTEGER_ZERO); // 0
System.out.println(NumberUtils.INTEGER_ONE); // 1
System.out.println(NumberUtils.INTEGER_TWO); // 2
System.out.println(NumberUtils.INTEGER_MINUS_ONE); // -1
3、Short value
// 3、Short value
System.out.println(NumberUtils.SHORT_ZERO); // 0
System.out.println(NumberUtils.SHORT_ONE); // 1
System.out.println(NumberUtils.SHORT_MINUS_ONE); // -1
Two 、 Compare the size
4、 Compare the size
// 4、 Compare the size
System.out.println(NumberUtils.compare(1, 10)); // -1
System.out.println(NumberUtils.compare(1L, 10L)); // -1
System.out.println(NumberUtils.compare('A', 'B')); // -1
3、 ... and 、 String rotation BigInteger and BigInteger
5、 String rotation BigInteger object
// 5、 String rotation BigInteger object
// explain :1、 Convert string to BigInteger, Support 10 Base number , Hexadecimal ( With 0X perhaps # start )、8 Base number ( With 0 start )
// explain :2、 If str by null, Then return to null. If the conversion is wrong , Throw out NumberFormatException abnormal
System.out.println(NumberUtils.createBigInteger("1000000000")); // 1000000000
6、 String rotation BigInteger object
// 6、 String rotation BigInteger object
// explain :1、 Convert string to BigDecimal, Only support 10 Base number , The bottom layer is to use new BigDecimal(str)
// explain :2、str by null when , return null,str If it is empty, it throws NumberFormatException abnormal
System.out.println(NumberUtils.createBigDecimal("1000000000")); // 1000000000
Four 、 String to number wrapper class
7、 String rotation Number object
// 7、 String rotation Number object
System.out.println(NumberUtils.createNumber("100")); // 100
8、 String rotation Integer object
// 8、 String rotation Integer object
System.out.println(NumberUtils.createInteger("100")); // 100
9、 String rotation Float object
// 9、 String rotation Float object
System.out.println(NumberUtils.createFloat("1.1")); // 1.1
10、 String rotation Double object
// 10、 String rotation Double object
System.out.println(NumberUtils.createDouble("1.1")); // 1.1
5、 ... and 、 Determine whether the string is related to the number
11、 Determine whether the string is a valid number
// 11、 Determine whether the string is a valid number
// Support :16 Base number 、8 Base number 、10 Base number 、 A positive number, a negative number 、 Scientific enumeration ( Such as 8.788006e+05)、 Type qualifier (110L、3.14f)
// str Is empty or null, All back to false
System.out.println(NumberUtils.isCreatable("100")); // true
System.out.println(NumberUtils.isCreatable("0XFF")); // true
System.out.println(NumberUtils.isCreatable("-1")); // true
System.out.println(NumberUtils.isCreatable("8.788006e+05")); // true
System.out.println(NumberUtils.isCreatable("110L")); // true
12、 Determine whether the string contains only numbers
// 12、 Determine whether the string contains only numbers
// Check whether the string contains only numbers , Both negative numbers and decimals are returned false
// str by null Or is empty return false
System.out.println(NumberUtils.isDigits("100")); // true
System.out.println(NumberUtils.isDigits("hello")); // false
System.out.println(NumberUtils.isDigits("")); // false
System.out.println(NumberUtils.isDigits(null)); // false
System.out.println(NumberUtils.isDigits("hello100world")); // false
13、 Judge whether the string can be parsed into numbers
// 13、 Judge whether the string can be parsed into numbers
// explain : Only support 10 Base number , Support positive and negative numbers , Supports decimals , No support 8 Base number 、16 Base number 、 Scientific counting is not supported , Nor does it support type qualifiers ( Such as 3000L,3.14F)
System.out.println(NumberUtils.isParsable("100")); // true
System.out.println(NumberUtils.isParsable("100.1")); // true
System.out.println(NumberUtils.isParsable("-1")); // true
System.out.println(NumberUtils.isParsable("8.788006e+05")); // false
System.out.println(NumberUtils.isParsable("3000L")); // false
System.out.println(NumberUtils.isParsable("hello100world")); // false
6、 ... and 、 Take the extreme value
14、 For maximum
// 14、 For maximum
// Support :byte、double、float、int、long、short
System.out.println(NumberUtils.max(1, 2, 3)); // 3
15、 For the minimum
// 15、 For the minimum
// Support :byte、double、float、int、long、short
System.out.println(NumberUtils.min(1, 2, 3)); // 1
7、 ... and 、 String to basic data type
16、 String rotation byte
// 16、 String rotation byte
// String rotation byte, If the conversion fails , After exception capture, it will not throw , Return the default value directly 0; If str by null, Returns the default value 0
System.out.println(NumberUtils.toByte("10")); // 10
System.out.println(NumberUtils.toByte("hello", (byte) 1)); // 1
17、 String rotation double
// 17、 String rotation double
System.out.println(NumberUtils.toDouble("1.1")); // 1.1
System.out.println(NumberUtils.toDouble("hello", 2L)); // 2.0
System.out.println(NumberUtils.toDouble(new BigDecimal("1000"))); // 1000.0
System.out.println(NumberUtils.toDouble(new BigDecimal("2"), 1L)); // 2.0
18、 String rotation float
// 18、 String rotation float
System.out.println(NumberUtils.toFloat("1.1")); // 1.1
System.out.println(NumberUtils.toFloat("hello", 2.2F)); // 2.2
19、 String rotation int
// 19、 String rotation int
System.out.println(NumberUtils.toInt("1")); // 1
System.out.println(NumberUtils.toInt("hello", 2)); // 2
20、 String rotation long
// 20、 String rotation long
System.out.println(NumberUtils.toLong("1")); // 1
System.out.println(NumberUtils.toLong("hello", 2L)); // 2
21、 String rotation short
// 21、 String rotation short
System.out.println(NumberUtils.toShort("1")); // 1
System.out.println(NumberUtils.toShort("hello", (short) 2)); // 2
8、 ... and 、 Complete code
package com.zibo.zibo2022.number_utils.main;
import org.apache.commons.lang3.math.NumberUtils;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
// start
// One 、 Common constants
// 1、Long value
System.out.println(NumberUtils.LONG_ZERO); // 0
System.out.println(NumberUtils.LONG_ONE); // 1
System.out.println(NumberUtils.LONG_MINUS_ONE); // -1
System.out.println(NumberUtils.LONG_INT_MIN_VALUE); // -2147483648
System.out.println(NumberUtils.LONG_INT_MAX_VALUE); // 2147483647
// 2、Integer value
System.out.println(NumberUtils.INTEGER_ZERO); // 0
System.out.println(NumberUtils.INTEGER_ONE); // 1
System.out.println(NumberUtils.INTEGER_TWO); // 2
System.out.println(NumberUtils.INTEGER_MINUS_ONE); // -1
// 3、Short value
System.out.println(NumberUtils.SHORT_ZERO); // 0
System.out.println(NumberUtils.SHORT_ONE); // 1
System.out.println(NumberUtils.SHORT_MINUS_ONE); // -1
// Two 、 Compare the size
// 4、 Compare the size
System.out.println(NumberUtils.compare(1, 10)); // -1
System.out.println(NumberUtils.compare(1L, 10L)); // -1
System.out.println(NumberUtils.compare('A', 'B')); // -1
// 3、 ... and 、 String rotation BigInteger and BigInteger
// 5、 String rotation BigInteger object
// explain :1、 Convert string to BigInteger, Support 10 Base number , Hexadecimal ( With 0X perhaps # start )、8 Base number ( With 0 start )
// explain :2、 If str by null, Then return to null. If the conversion is wrong , Throw out NumberFormatException abnormal
System.out.println(NumberUtils.createBigInteger("1000000000")); // 1000000000
// 6、 String rotation BigInteger object
// explain :1、 Convert string to BigDecimal, Only support 10 Base number , The bottom layer is to use new BigDecimal(str)
// explain :2、str by null when , return null,str If it is empty, it throws NumberFormatException abnormal
System.out.println(NumberUtils.createBigDecimal("1000000000")); // 1000000000
// Four 、 String to number
// 7、 String rotation Number object
System.out.println(NumberUtils.createNumber("100")); // 100
// 8、 String rotation Integer object
System.out.println(NumberUtils.createInteger("100")); // 100
// 9、 String rotation Float object
System.out.println(NumberUtils.createFloat("1.1")); // 1.1
// 10、 String rotation Double object
System.out.println(NumberUtils.createDouble("1.1")); // 1.1
// 5、 ... and 、 Determine whether the string is related to the number
// 11、 Determine whether the string is a valid number
// Support :16 Base number 、8 Base number 、10 Base number 、 A positive number, a negative number 、 Scientific enumeration ( Such as 8.788006e+05)、 Type qualifier (110L、3.14f)
// str Is empty or null, All back to false
System.out.println(NumberUtils.isCreatable("100")); // true
System.out.println(NumberUtils.isCreatable("0XFF")); // true
System.out.println(NumberUtils.isCreatable("-1")); // true
System.out.println(NumberUtils.isCreatable("8.788006e+05")); // true
System.out.println(NumberUtils.isCreatable("110L")); // true
// 12、 Determine whether the string contains only numbers
// Check whether the string contains only numbers , Both negative numbers and decimals are returned false
// str by null Or is empty return false
System.out.println(NumberUtils.isDigits("100")); // true
System.out.println(NumberUtils.isDigits("hello")); // false
System.out.println(NumberUtils.isDigits("")); // false
System.out.println(NumberUtils.isDigits(null)); // false
System.out.println(NumberUtils.isDigits("hello100world")); // false
// 13、 Judge whether the string can be parsed into numbers
// explain : Only support 10 Base number , Support positive and negative numbers , Supports decimals , No support 8 Base number 、16 Base number 、 Scientific counting is not supported , Nor does it support type qualifiers ( Such as 3000L,3.14F)
System.out.println(NumberUtils.isParsable("100")); // true
System.out.println(NumberUtils.isParsable("100.1")); // true
System.out.println(NumberUtils.isParsable("-1")); // true
System.out.println(NumberUtils.isParsable("8.788006e+05")); // false
System.out.println(NumberUtils.isParsable("3000L")); // false
System.out.println(NumberUtils.isParsable("hello100world")); // false
// 6、 ... and 、 Take the extreme value
// 14、 For maximum
// Support :byte、double、float、int、long、short
System.out.println(NumberUtils.max(1, 2, 3)); // 3
// 15、 For the minimum
// Support :byte、double、float、int、long、short
System.out.println(NumberUtils.min(1, 2, 3)); // 1
// 7、 ... and 、 String to basic data type
// 16、 String rotation byte
// String rotation byte, If the conversion fails , After exception capture, it will not throw , Return the default value directly 0; If str by null, Returns the default value 0
System.out.println(NumberUtils.toByte("10")); // 10
System.out.println(NumberUtils.toByte("hello", (byte) 1)); // 1
// 17、 String rotation double
System.out.println(NumberUtils.toDouble("1.1")); // 1.1
System.out.println(NumberUtils.toDouble("hello", 2L)); // 2.0
System.out.println(NumberUtils.toDouble(new BigDecimal("1000"))); // 1000.0
System.out.println(NumberUtils.toDouble(new BigDecimal("2"), 1L)); // 2.0
// 18、 String rotation float
System.out.println(NumberUtils.toFloat("1.1")); // 1.1
System.out.println(NumberUtils.toFloat("hello", 2.2F)); // 2.2
// 19、 String rotation int
System.out.println(NumberUtils.toInt("1")); // 1
System.out.println(NumberUtils.toInt("hello", 2)); // 2
// 20、 String rotation long
System.out.println(NumberUtils.toLong("1")); // 1
System.out.println(NumberUtils.toLong("hello", 2L)); // 2
// 21、 String rotation short
System.out.println(NumberUtils.toShort("1")); // 1
System.out.println(NumberUtils.toShort("hello", (short) 2)); // 2
// end
}
}
边栏推荐
- [network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
- Rk3399 9.0 driver add powser button
- 【无标题】
- 会议OA项目之会议通知&会议反馈&反馈详情功能
- 新一代超安全蜂窝电池,思皓爱跑上市,13.99万起售
- 用CDO进行nc数据的不规则裁剪
- Error reporting: Rong Lianyun sends SMS verification code message 500
- Yield Guild Games:这一年的总结与未来展望
- 【commons-lang3专题】004- NumberUtils 专题
- (20211130更新)关于jupyter notebook的下载安装及自己的配置、主题
猜你喜欢

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告

将Word中的表格以图片形式复制到微信发送

Error reporting: Rong Lianyun sends SMS verification code message 500

【Web开发】Flask框架基础知识

Meeting notification & meeting feedback & feedback details function of meeting OA project

AQS原理

selenium对接代理与seleniumwire访问开发者工具NetWork

Data warehouse construction - ads floor

面试突击69:TCP 可靠吗?为什么?

自制 | 纯手工自制一个16位RISC架构CPU
随机推荐
[raspberry pie] how does the windows computer connect with raspberry pie
时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
Requestvideoframecallback() simple instance
Station B "crashed" from beginning to end 2021.07.13 we collapsed like this (Reprint)
Outlier detection and open set identification (1)
What opportunities does the London gold real-time market bring?
浅谈一下跨端技术方案
【无标题】
状态压缩dp-蒙德里安的梦想
用CDO进行nc数据的不规则裁剪
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(8)毕业设计论文模板
Wechat campus bathroom reservation of small program completion work (6) opening defense ppt
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
Anomaly detection and unsupervised learning (1)
Isolation level of MySQL, possible problems (dirty reading, unrepeatable reading, phantom reading) and their solutions
B+ tree~
PLATO上线LAAS协议Elephant Swap,用户可借此获得溢价收益
【commons-lang3专题】005- ObjectUtils 专题
[AD learning] the course of PCB drawing in this marine vehicle competition
Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency