java成神之——数值操作BigDecimal,BigInteger,Random,SecureRandom
数值操作
数值新特性
123_456 等价于 123456,增加可读性
包装类
每个基本数据类型都会有一个包装类与之对应,用来提供更为强大的功能
包装类: Byte Short Integer Long Float Double Character Boolean
将字符串转化成对应的基本数据类型
boolean bl = Boolean.parseBoolean("false");
int i = Integer.parseInt("11");
将基本数据类型转化成字符串
String str = Integer.toString(3);
使用包装类构造函数
Integer it = new Integer("5555");
int i = it.intValue();
基本数据类型自动装箱和拆箱
Integer i = 11; // 装箱
i++; // 拆箱
浮点
String.format("%.2f", 1.2399); // "1.24"
new DecimalFormat("0.##").format(1.2323000); // 1.23
BigDecimal
高精度计算
BigDecimal方法
BigDecimal a = new BigDecimal(10000000);
BigDecimal b = new BigDecimal(10000001);
a.compareTo(b) // -1 a>b -1, a=b 0, a<b 1
a.add(b) // 20000001
a.subtract(b) // -1
a.multiply(b) // 100000010000000
a.multiply(b, new MathContext(4,RoundingMode.FLOOR)) // 1.000E+14
a.divide(b, new MathContext(4,RoundingMode.HALF_DOWN)) // 1.000
a.divide(b, 4, RoundingMode.HALF_DOWN) // 1.000
a.remainder(b) // 10000000 取模
a.pow(10) // 次方
a.max(b) // 比较a,b谁大
a.min(b) // 比较a,b谁小
a.movePointLeft(2) // 100000.00 移动小数点
不要使用equals判断是否相等,除非数字小数点个数也相同
BigDecimal静态实例
BigDecimal a = BigDecimal.ZERO;
BigDecimal b = BigDecimal.ONE;
BigDecimal c = BigDecimal.TEN;
String.format("%s, %s, %s", a, b, c) // 0, 1, 10
BigInteger
高精度计算
BigDecimal方法
BigInteger a = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger b = new BigInteger("-11");
BigInteger c = BigInteger.ZERO;
BigInteger d = BigInteger.ONE;
BigInteger e = BigInteger.TEN;
String.format("%s, %s, %s, %s, %s", a, b, c, d, e) // 9223372036854775807, -11, 0, 1, 10
a.add(b);
a.subtract(b);
a.divide(b);
a.multiply(b);
a.pow(3);
a.remainder(b);
a.max(b);
a.min(b);
a.equals(b);
不要使用 == 比较
a.compareTo(b);
返回一个64位数
BigInteger a = new BigInteger(32, new Random()); // 随机数,32表示位数,因为返回的是正数所以一共64位
BigInteger b = new BigInteger(32, new SecureRandom()); // 性能低,但是随机数质量更高
数值本地化
ISO 639 alpha-2 语言简写 https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
ISO 3166 alpha-2 国家简写 https://www.iso.org/obp/ui/#search
格式化数值
Locale locale = new Locale("zh", "CN");
NumberFormat numberFormat = NumberFormat.getInstance(locale);
numberFormat.format(1.111)
格式化货币
Locale locale = new Locale("zh", "CN");
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
numberFormat.format(1.111) // ¥1.11
格式化百分数
Locale locale = new Locale("zh", "CN");
NumberFormat numberFormat = NumberFormat.getPercentInstance(locale);
numberFormat.format(1.111) // 111%
格式化位数
Locale locale = new Locale("zh", "CN");
NumberFormat numberFormat = NumberFormat.getInstance(locale);
numberFormat.setMinimumIntegerDigits(2);
numberFormat.format(1.111); // 01.111
numberFormat.setMaximumIntegerDigits(1);
numberFormat.format(12.111); // 2.111
numberFormat.setMinimumFractionDigits(1);
numberFormat.format(12); // 12.0
numberFormat.setMaximumFractionDigits(1);
numberFormat.format(12.11) // 12.1
随机数
假随机数
Random random = new Random();
int randInt = random.nextInt(); // 0-1
int randInt = random.nextInt(1000); // 0-1000
ThreadLocalRandom.current().nextInt(10, 100); // 10-100
long randLong = random.nextLong();
double randDouble = random.nextDouble();
float randFloat = random.nextFloat();
byte[] bytes = new byte[16];
random.nextBytes(bytes);
真随机数
SecureRandom rng = new SecureRandom();
byte[] randomBytes = new byte[64];
rng.nextBytes(randomBytes);
Arrays.toString(randomBytes);
播种
相同的种子只能产生相同的随机数
Random random = new Random(12345L);
Random random = new Random(System.currentTimeMillis());
ThreadLocalRandom tlr = ThreadLocalRandom.current().setSeed(12345L);
ThreadLocalRandom.current().setSeed(System.currentTimeMillis());
结语
本文章是java成神的系列文章之一
如果你想知道,但是本文没有的,请下方留言
我会第一时间总结出来并发布填充到本文
java成神之——数值操作BigDecimal,BigInteger,Random,SecureRandom的更多相关文章
- java成神之——线程操作
线程 Future CountDownLatch Multithreading synchronized Thread Producer-Consumer 获取线程状态 线程池 ThreadLocal ...
- java成神之——jaxb操作xml的基本使用
JAXB 依赖 读取xml配置 写配置 自定义写配置 结语 JAXB 依赖 <dependency> <groupId>javax.activation</groupId ...
- java成神之——enum枚举操作
枚举 声明 枚举遍历 枚举在switch中使用 枚举比较 枚举静态构造方法 使用类来模拟枚举 枚举中定义抽象方法 枚举实现接口 单例模式 使用静态代码快 EnumSet EnumMap 结语 枚举 声 ...
- java成神之——Stream和Optional
Stream流 基本使用 流关闭 平行流 流重用 iterator转换成流 分组计数 无限流 流转集合 压缩流 统计数值流 集合转换流遍历 流拼接 reduce 使用流生成随机字符串 流的包装流 几种 ...
- java成神之——java中string的用法
java中String的用法 String基本用法 String分割 String拼接 String截取 String换行符和format格式化 String反转字符串和去除空白字符 String获取 ...
- java成神之——MySQL Connector/J 的基本使用
使用示例 DBCP连接池 结语 使用示例 public class demo { static Connection con = null; static Statement st = null; s ...
- java成神之——文件IO
文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStrea ...
- java成神之——正则表达式基本使用
正则表达式 常用匹配规则 基本使用 标记符的使用 部分正则标记 正则表达式在字符串方法中的使用 结语 正则表达式 常用匹配规则 [abc] abc其中一个 [^abc] abc之外的一个 [a-z] ...
- java成神之——ImmutableClass,null检查,字符编码,defaultLogger,可变参数,JavaScriptEngine,2D图,类单例,克隆,修饰符基本操作
ImmutableClass null检查 字符编码 default logger 函数可变参数 Nashorn JavaScript engine 执行脚本文件 改变js文件输出流 全局变量 2D图 ...
随机推荐
- sql server数据库课程设计分析
课题:能源管理收费系统 系统功能的基本要求: (1)用户基本信息的录入:包括用户的单位.部门.姓名.联系电话.住址 : (2)用户水.电.气数据的录入(每个月的数据的录入): (3)水.电.气价格的管 ...
- iOS调试技巧(debug)
说到debug,可以说到的东西就太多了,一个程序员,即使逻辑非常出色,也会出现bug问题,那么debug是每个程序员必备的技巧,尤其是Xcode开发, 苹果公司的开发的Xcode真的是十分强大 ...
- 关于inline函数
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4975474.html 今天我在优化公司项目代码的过程中,借助了Intel的VTune ...
- TCPL学习笔记:编写expand(s1, s2),将字符串s1中类似于a-z一类的速记符号在s2中扩充完整。可以处理大小写及字符,以及a-b-c, a-z0-9以及-a-z等多种情况。
话不多说,看代码: #include <stdio.h> #include <stdlib.h> int main(void) { ] = "a-z0-9hahah- ...
- linux简单介绍,helloworld,vi使用,用户管理
linux特点1.免费的.开源的2.支持多线程.多用户的3.安全性好4.对内存和文件管理优越 缺点:操作相对困难 linux最小只需要4m -> 嵌入式开发 我们使用 vm[虚拟机] 虚拟了一个 ...
- Django应用部署
前言 Apachewsgi 环境搭建 安装Apache 安装mod_wsgi 添加djangowsgi文件 配置etcapache2httpdconf wsgipy配置 跑起来吧 uWSGI 环境搭建 ...
- 如何回复在Git中误操作删除的文件?
不小心的操作,删除了某些文件 某天很高大地用上了git来管理版本,分布式版本管理工具,好是好,但是还不是很熟悉,结果某天一不小心就从本地仓库中删除了若干个文件,该如何找回这些文件呢? 调查现场 git ...
- Matisse,来自知乎的PhotoPicker
简介 Matisse,是一款由知乎开源的媒体选择器. 在Activity和Fragment中使用 支持JPEG,PNG,GIF的图片选择和MPEG,MP4格式的视频选择.不能同时选择图片和视频 两种主 ...
- keras中自定义Layer
最近在学习SSD的源码,其中有两个自定的层,特此学习一下并记录. import keras.backend as K from keras.engine.topology import InputSp ...
- 【个人吐槽】C、Delphi、C#、java 摘抄
作为个人的一个感受就是,在win平台上开发软件,别再他妈的用MFC了,不适合新手,上手太难.你妹,实现一个半透明的功能,一堆代码,而C#就他妈的几行话.靠. 似乎很多人都觉得Delphi已经没落了.过 ...