栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习 数字处理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习 数字处理

文章目录
        • 数字格式化
        • math
        • 随机数
        • 大数字运算
          • BigInteger
          • BigDecimal


数字格式化
- 数字表示原则【没有格式化的数据】
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 DecimalForamt(pattern)
    • String outs = myFormat.format(data)
  • 二、
    • DecimalFormat myFormat = new DecimalFormat()
    • myFormat.applyPatern(pattern) —here
    • String outs = myFormat.format(data)

  • 笔记
  • 0 —不足则显示补0; # 不足则不显示
  • 可以在pattern末尾添加字符(单位。。。等)
  • u2030千分号
  • 12e2 同样可以这样表示数据

  • 分组
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
  • 常量
    • Math.PI π
    • Math.E e
  • 函数
三角指数
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)
随机数
  • Math.random()
范围语法
int_1 ~ int_2num = (int)int_1 + (int)(Math.random()*(int_2-int_1))
double_1 ~double_2num = (double)double_1 + (double)(Math.random()*(double_2-duble_1))
chr1 ~ chr2chr = (char)chr1 + (char)(Math.random()*(chr2 - chr1 + 1)

  • Random类

引用库

import java.util.Random;

定义

Random r = new Random();
// or
Random r = new Random(seed);
  • nextInt(n) [0, n) of Int
  • nextLong()
  • nextBoolean()
  • nextFloat()
  • nextDouble()
  • nextGaussian() 高斯分布的 -double
    • 生成 num~num1的浮点数
      (double)num + (double)(r.nextDouble()*(num1-num)

大数字运算
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类似

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/331939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号