数字类 DecimalFormat 和 BigDecimal 和 random 随机数
2019-05-22 17:30
211 查看
# 任意数字
,千分位
. 小数点
0 不够补0
[code]public static void main(String[] args) { //数字格式化 DecimalFormat df = new DecimalFormat("### ,###"); System.out.println(df.format(1234567)); //将一个数字转化为String DecimalFormat df1 = new DecimalFormat("### ,###.##"); System.out.println(df1.format(1234567.123));//加入千分位,保留两位小数 DecimalFormat df2 = new DecimalFormat("### ,###.0000");//保留四位小数,不够补0 System.out.println(df2.format(123456.123)); }
数字精确度很高的BigDecimal
[code]public static void main(String[] args) { BigDecimal bg = new BigDecimal(10); BigDecimal bg1 = new BigDecimal(20); //两个引用类型不能直接做加法运算 BigDecimal a =bg.add(bg1); System.out.println(a);//30 }
随机数 random
[code]public static void main(String[] args) { Random r = new Random(); for(int k = 0;k<5;k++) { System.out.println(r.nextInt(100)); } //int i = r.nextInt(101);//[0-100] }
生成五个不重复的随机数
[code]public static void main(String[] args) { Random r = new Random(); int a[] = new int[5]; int index = 0; while(index<5) { System.out.println("!!!!!"); int temp = r.nextInt(6); if (temp != 0&&!contains(a,temp)) {//不等于0,且不包含 System.out.println("@@@@@@"); a[index++] = temp; } } for(int i = 0;i<a.length;i++) { System.out.println(a[i]); } } //该方法完成判断temp元素在a数组中是否包含 public static boolean contains(int []a,int temp) { for(int i =0;i<a.length;i++) { if(a[i] == temp) { return true; } } return false; }
相关文章推荐
- Java常用类库--Math、Random、NumberFormat、DecimalFormat、BigInteger、BigDecimal
- 基本数据类型装箱拆箱,与转换方法,BigDecimal运算,Random随机数
- C#进阶 14 Continue 三元表达式 跟 Random 随机数
- Random产生随机数与Math.random()产生随机数的比较
- 用Math.random()方法生成随机数
- [erlang] 为什么叫random的随机数是伪随机数
- [转载]C# Random 生成不重复随机数
- 有意思的随机数 Random
- c# Random太快产生的随机数会重复
- [功能帮助类] C# BaseRandom随机数,随机字符,可限制范围-帮助类 (转载)
- Verilog $random用法 随机数
- Random-0至100间的50个随机数
- Python模块学习--random 随机数生成
- Python标准库12 数学与随机数 (math包,random包)
- Random随机数某一时刻生成相同数问题(IBM JDK和SUN JDK差矣造成)
- Python标准库12 数学与随机数 (math包,random包)
- [python] random 库 -- 生成随机数/打乱序列
- python 中生成随机数(random)
- random()方法获得一定范围内的随机数
- java常用类解析十一:Random类(Math.random())生成指定范围的随机数或字符