当前位置:网站首页>[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
}
}
边栏推荐
- 分类预测 | MATLAB实现TCN时间卷积神经网络的时序分类预测
- Relying on cloud business to support revenue growth alone, is Microsoft still overvalued?
- mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]
- JWT token related configuration (global configuration identity authentication rewrites authenticate method)
- Api 接口优化的那些技巧
- 伦敦金即时行情带来什么机会?
- 从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
- Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency
- What opportunities does the London gold real-time market bring?
- [raspberry pie] how does the windows computer connect with raspberry pie
猜你喜欢

Method of converting inline elements to block elements

追踪伦敦银实时行情的方法

Talk about the cross end technical scheme

【Web开发】Flask框架基础知识
![[AD learning] the course of PCB drawing in this marine vehicle competition](/img/37/211a0557848f6922fda7a69a114923.png)
[AD learning] the course of PCB drawing in this marine vehicle competition

mysql存储过程 实现创建一张表(复制原表的结构新建的表)

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

Some considerations about ThreadPool

Hash table~

追踪伦敦银实时行情的方法有哪些?
随机推荐
What opportunities does the London gold real-time market bring?
多线程顺序运行的几种方法,面试可以随便问
【无标题】
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
Common sparse basis and matlab code for compressed sensing
Hash table~
Station B "crashed" from beginning to end 2021.07.13 we collapsed like this (Reprint)
Outlier detection and open set identification (1)
JWT token related configuration (global configuration identity authentication rewrites authenticate method)
Outlier detection and open set identification (2)
CUDA related
面试突击69:TCP 可靠吗?为什么?
Protective copy & stateless
iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器
如何在WordPress中创建一个自定义404错误页面
Necessary interview skills for Android (including interview questions and learning materials)
靠云业务独撑收入增长大梁,微软仍然被高估?
新拟态个人引导页源码
COPU陆首群教授应邀在开放原子全球开源峰会上做主旨演讲
mysql时间按小时格式化_mysql时间格式化,按时间段查询的MySQL语句[通俗易懂]