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);
}
}

最新文章

  1. Struts2入门(四)——数据输入验证
  2. Tomcat服务器重启失败:The server may already be running in another process, or a system process may be using the port.
  3. C++函数重载和函数模板
  4. Chrome扩展程序的二次开发:把它改得更适合自己使用
  5. Android虚拟机Classic qemu does not support SMP问题记录
  6. mysql常用函数整理
  7. enum to IEnumerable&lt;T&gt;
  8. 【linux操作命令】crontab
  9. position与anchorPoint
  10. express4.x 路由中间件
  11. C / C++算法学习笔记(7)-双向冒泡
  12. MariaDb数据库管理系统的学习(一)安装示意图
  13. JUnit01Eclipse添加JUnite
  14. 使用FusionCharts出柱状图和饼状图
  15. Linux平台使用指令记录
  16. xml中的SQL注入
  17. Tornado框架实现图形验证码功能
  18. js 随机生成颜色值
  19. BZOJ3676 APIO2014回文串(manacher+后缀自动机)
  20. Ubuntu下安装LXC手记之centos

热门文章

  1. Redis(6379)未授权访问
  2. 键盘和鼠标闲置超时时关闭显示器并锁定电脑桌面的AutoHotkey脚本 2019年11月24日写
  3. 用Python预测双色球福利彩票中奖号码(请不要当真)
  4. Dapr 客户端 搭配 WebApiClientCore 玩耍服务调用
  5. Calcite(一):javacc语法框架及使用
  6. Spring Boot实现数据访问计数器
  7. 数据库比对工具SQL(表、字段、触发器、索引、视图、存储过程)
  8. Spring WebFlux 基础教程:WebSocket 使用
  9. Upload-labs 文件上传靶场通关攻略(上)
  10. 新手安装eclipse或idea后进行配置、快捷键、插件总结