文章目录
数字格式化
- 数字表示原则【没有格式化的数据】 1. 绝对值 > .001 & < 1e7,以常规小数显示; 2. 绝对值 <.001 or >1e7,用科学计数法表示 |
| 字符 | 说明 |
|---|
| 0 | 代表一位数字,该位不存在则显示0 |
| # | 代表一位数字,该位不存在则不显示 |
| . | 小数分隔符、货币小数分隔符 |
| - | 负号 |
| , | 分组分隔符 |
| E | 分隔科学计数法中的位数和指数 |
| % | 前缀或后缀,百分制显示 |
| u2030 | 前缀或后缀,千分制‰表示 |
| u00A4 | 前缀或后缀,货币记号 |
| ’ | 单引号。为特殊符号添加单引号 |
import java.text.DecimalFormat
public class hello{
public static void main(String[] args){
hello h = new hello();
h.formatTest();
}
void formatTest(){
formatPirnt("000.000", 1234.2); //1234.200
formatPrint("###.###Kg", 1234.2); //1234.2Kg
formatPrint("#.##%", 0.123); //12.3%
formatPrint("#.##u2030", 0.123); //123‰
formatPrint("", 23E13); //230,000,000,000,000
}
void formatPrint(String pattern, double data){
DecimalFormat myFormat = new DecimalForamt(pattern);
String outs = myFormat.format(data);
System.out.println(value + ' ' + pattern + ' ' + outs);
}
void formatPrint_1(String pattern, double data){
DecimalFormat myFormat = new DecimalFormat();
myFormat.applyPatern(pattern);
String outs = myFormat.format(data);
System.out.println(value + ' ' + pattern + ' ' + outs);
}
}
DecimalFormat myFormat = new DecimalFormat();
myFormat.setGroupSize(3);
System.out.println(myFormat.format(12345231456.123e2));
// Out>>> 1,234,523,145,612.3
myFormat.setGroupingUsed(flase); // 取消分组
math
| 三角 | 指数 |
|---|
| sin() | exp() |
| cos() | log() |
| tan() | log10() |
| asin() | sqrt() |
| acos() | cbrt() 立方根 |
| atan() | pow(a,b)
a
b
a^b
ab |
| toRadians() | |
| toDegrees() | |
| 取整函数 | 说明 |
|---|
| ceil() | 向上取整 |
| floor() | 向下取整 |
| rint() | 取最接近,上下同样接近则取偶数 rint(1.5) = 2 |
| round() | 四舍五入 |
| 最大最小绝对值 |
|---|
| max(a,b) |
| min(a,b) |
| abs(a) |
随机数
| 范围 | 语法 |
|---|
| int_1 ~ int_2 | num = (int)int_1 + (int)(Math.random()*(int_2-int_1)) |
| double_1 ~double_2 | num = (double)double_1 + (double)(Math.random()*(double_2-duble_1)) |
| chr1 ~ chr2 | chr = (char)chr1 + (char)(Math.random()*(chr2 - chr1 + 1) |
引用库
import java.util.Random;
定义
Random r = new Random();
// or
Random r = new Random(seed);
大数字运算
BigInteger
BigInteger one = new BigInteger("23")
| 运算 | 语法 |
|---|
| 加 | add(value) |
| 减 | subtract(value) |
| 乘 | multiply(value) |
| 除 | divid(value) |
| 取余 | remainder(value) |
| 除法 | dividAndRemainder(value) return [商,余数] |
| 幂运算 | pow(value) |
| 取反 | negate() |
| 左移 | shiftLeft(n) |
| 右移 | shiftRight(n) |
| 与操作 | and(value) |
| 或操作 | or(value) |
| 比较 | compareTo(value) |
| 判断相同 | equals(vlaue) |
| 最小 | min(a,b) |
| 最大 | max(a,b) |
BigDecimal
BigDecimal b = new BigDecimal();
方法跟BigInteger类似