java一些工具类
2024-10-19 12:24:22
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* @author: tomingto
*
**/
public class NumberUtils { public static boolean isInt(Double num) {
return num.intValue() == num;
} /**
* 判断字符串是否是数值格式
* @param str
* @return
*/
public static boolean isDigit(String str){
if(str == null || str.trim().equals("")){
return false;
}
return str.matches("^\\d+$");
} /**
* 将一个小数精确到指定位数
* @param num
* @param scale
* @return
*/
public static double scale(double num, int scale) {
BigDecimal bd = new BigDecimal(num);
return bd.setScale(scale, RoundingMode.HALF_UP).doubleValue();
} // 从字符串中根据正则表达式寻找,返回找到的数字数组
public static Double[] searchNumber(String value, String regex){
List<Double> doubles = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
if(matcher.find()) {
MatchResult result = matcher.toMatchResult();
for (int i = 1; i <= result.groupCount(); i++) {
doubles.add(Double.valueOf(result.group(i)));
}
}
return doubles.toArray(new Double[doubles.size()]);
} /**
* 生成指定位数的随机数字
* @param len
* @return
*/
public static String generateCode(int len){
len = Math.min(len, 8);
int min = Double.valueOf(Math.pow(10, len - 1)).intValue();
int num = new Random().nextInt(Double.valueOf(Math.pow(10, len + 1)).intValue() - 1) + min;
return String.valueOf(num).substring(0,len);
}
}
最新文章
- Struts2入门(四)——数据输入验证
- Tomcat服务器重启失败:The server may already be running in another process, or a system process may be using the port.
- C++函数重载和函数模板
- Chrome扩展程序的二次开发:把它改得更适合自己使用
- Android虚拟机Classic qemu does not support SMP问题记录
- mysql常用函数整理
- enum to IEnumerable<;T>;
- 【linux操作命令】crontab
- position与anchorPoint
- express4.x 路由中间件
- C / C++算法学习笔记(7)-双向冒泡
- MariaDb数据库管理系统的学习(一)安装示意图
- JUnit01Eclipse添加JUnite
- 使用FusionCharts出柱状图和饼状图
- Linux平台使用指令记录
- xml中的SQL注入
- Tornado框架实现图形验证码功能
- js 随机生成颜色值
- BZOJ3676 APIO2014回文串(manacher+后缀自动机)
- Ubuntu下安装LXC手记之centos
热门文章
- Redis(6379)未授权访问
- 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写
- 用Python预测双色球福利彩票中奖号码(请不要当真)
- Dapr 客户端 搭配 WebApiClientCore 玩耍服务调用
- Calcite(一):javacc语法框架及使用
- Spring Boot实现数据访问计数器
- 数据库比对工具SQL(表、字段、触发器、索引、视图、存储过程)
- Spring WebFlux 基础教程:WebSocket 使用
- Upload-labs 文件上传靶场通关攻略(上)
- 新手安装eclipse或idea后进行配置、快捷键、插件总结