bigdecimal

17_常用API_第17天(包装类、System、Math、Arrays、大数据运算)_讲义

依然范特西╮ 提交于 2020-01-19 11:52:29
今日内容介绍 1、基本类型包装类 2、System类 3、Math类 4、Arrays类 5、大数据运算 01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。 而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型, 如年龄需要转换成int类型,考试成绩需要转换成double类型等 *b.八种基本类型对应的包装类 char Character int Integer byte Byte short Short long Long float Float double Double boolean Boolean 02Integer类parseInt方法 *A:Integer类parseInt方法: *a:parseInt() int i = Integer.parseInt("12"); System.out.println(i/2);//6 *b:parseInt(String s, int radix) /* * Integer类静态方法parseInt(String s, int radix) * radix基数,进制 * "110",2 含义 前面的数字是二进制的,但是方法parseInt运行结果都是十进制 * 指定进制的字符串转换为十进制的整数 *

Java 基础 常用API (System类,Math类,Arrays, BigInteger,)

旧时模样 提交于 2020-01-19 11:51:50
基本类型包装类   基本类型包装类概述     在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等。那么,想实现字符串与基本数据之间转换怎么办呢?     Java中提供了相应的对象来解决该问题,基本数据类型对象包装类:java将基本数据类型值封装成了对象。封装成对象有什么好处?可以提供更多的操作基本数值的功能。 8种基本类型对应的包装类如下:     其中需要注意int对应的是Integer,char对应的Character,其他6个都是基本类型首字母大写即可。     基本数据类型对象包装类特点:用于在基本数据和字符串之间进行转换。     将字符串转成基本类型:   parseXXX(String s);其中XXX表示基本类型,参数为可以转成基本类型的字符串,如果字符串无法转成基本类型,将会发生数字转换的问题 NumberFormatException System.out.println(Integer.parseInt("123") + 2); //打印结果为 125   将基本数值转成字符串有3种方式:   基本类型直接与””相连接即可;34+""   调用String的valueOf方法; String

BigDecimal类

喜欢而已 提交于 2020-01-18 18:53:43
1.重要意义 1.1.浮点数计算出错 1 public class Main { 2 public static void main(String[] args) { 3 System.out.println(2.0-1.1); 4 } 5 } 1 0.8999999999999999   原因分析:浮点数值不适用于无法接受舍入误差的金融计算中。这种舍入误差的主要原因是浮点数值采用的二进制系统表示,而在二进制系统中无法精确地表示分数1/10.这点就好想十进制无法精确地表示分数1/3一样。如果在数值计算中不允许有任何舍入误差,就应该使用BigDecimal类。 来源: https://www.cnblogs.com/yangyh26/p/12209830.html

数字、字符串、时间、格式化类常用函数解读——JAVA

≯℡__Kan透↙ 提交于 2020-01-18 02:59:35
目录 一、数字类 1.1超大数浮点数 1.2超大数整数 1.3随机数: 1.4格式化输出 1.5浮点数和双精度 二、字符串 2.1String 2.2正则表达式的简单说明: 2.3可变字符串StringBuffer/StringBuilder 三、时间类 3.1Calendar 四、格式化类 4.1数字格式化DecimalFormat 4.2字符串格式化 4.3时间格式化 4.3.1DateFormat 4.3.2LocalDate 一、数字类 1.1超大数浮点数 BigDecimal b1 = new BigDecimal("123456789.987654321"); // 声明BigDecimal对象 BigDecimal b2 = new BigDecimal("987654321.123456789"); // 声明BigDecimal对象 System.out.println("b1: " + b1 + ", b2:" + b2); System.out.println("加法操作:" + b2.add(b1)); // 加法操作 System.out.println("减法操作:" + b2.subtract(b1)); // 减法操作 System.out.println("乘法操作:" + b2.multiply(b1)); // 乘法操作 //需要指定位数

Appropriate scale for converting via BigDecimal to floating point

久未见 提交于 2020-01-15 06:01:35
问题 I've written an arbitrary precision rational number class that needs to provide a way to convert to floating-point. This can be done straightforwardly via BigDecimal: return new BigDecimal(num).divide(new BigDecimal(den), 17, RoundingMode.HALF_EVEN).doubleValue(); but this requires a value for the scale parameter when dividing the decimal numbers. I picked 17 as the initial guess because that is approximately the precision of a double precision floating point number, but I don't know whether

lambda表达式修改外部变量

China☆狼群 提交于 2020-01-14 19:45:55
lambda 表达式接受外部变量是通过copy副本的方式,所以编译器要求不能修改该变量值或则引用值,如果要修改可以通过集合或则实体类包装的方式实现。 如: https://zhuanlan.zhihu.com/p/29245059 public static void main(String[] args) { System.out.println("sss"); List<String> list = Arrays.asList("0.1", "1.22", "22", "321"); List<BigDecimal> bigDecimal = new ArrayList<>(); list.forEach(u->{ if (CollectionUtils.isEmpty(bigDecimal)) { bigDecimal.add(new BigDecimal(u)); } else { bigDecimal.set(0, bigDecimal.get(0).add(new BigDecimal(u))); } }); System.out.println(bigDecimal.toString()); } 来源: CSDN 作者: 拉丁卡特 链接: https://blog.csdn.net/u013217730/article/details/103973877

Java: BigDecimal and Double.NaN

∥☆過路亽.° 提交于 2020-01-14 08:05:51
问题 I am trying to execute following code: import java.math.*; public class HelloWorld{ public static void main(String []args){ System.out.println(BigDecimal.valueOf(Double.NaN)); } } And reasonably, I am getting: Exception in thread "main" java.lang.NumberFormatException at java.math.BigDecimal.<init>(BigDecimal.java:470) at java.math.BigDecimal.<init>(BigDecimal.java:739) at java.math.BigDecimal.valueOf(BigDecimal.java:1069) at HelloWorld.main(HelloWorld.java:6) Is there a way to represent

Java: BigDecimal and Double.NaN

做~自己de王妃 提交于 2020-01-14 08:05:13
问题 I am trying to execute following code: import java.math.*; public class HelloWorld{ public static void main(String []args){ System.out.println(BigDecimal.valueOf(Double.NaN)); } } And reasonably, I am getting: Exception in thread "main" java.lang.NumberFormatException at java.math.BigDecimal.<init>(BigDecimal.java:470) at java.math.BigDecimal.<init>(BigDecimal.java:739) at java.math.BigDecimal.valueOf(BigDecimal.java:1069) at HelloWorld.main(HelloWorld.java:6) Is there a way to represent

Forcing BigDecimals to use scientific notation

ぃ、小莉子 提交于 2020-01-10 19:59:12
问题 I have this method: public void Example(BigDecimal value, int scale){ BigDecimal x = new BigDecimal("0.00001"); System.out.println("result: " + (value.multiply(x)).setScale(scale, RoudingMode.HALF_UP).toString()); If, per example, value = 1 and scale = 2, the output is "result: 0.00". I thought it would be 1.00E-5. So, my doubt is: How can I force a BigDecimal to be formated in scientific notation if its scale is bigger than a certain value (it was 2 in my example) ? 回答1: You can use a

BigDecimal to the power of BigDecimal on Java/Android

淺唱寂寞╮ 提交于 2020-01-09 08:08:06
问题 I have a simple BigDecimal that I want to power to another BigDecimal, for example 11.11^-1.54. What would be the neatest way to do it in Java/Android? I don't like the idea of converting to doubles because it is for a medical application, so I would appreciate the biggest presicion possible. So far I have reviewed http://commons.apache.org/math/ and math stuff from Google Guava, but found nothing. Edit: The whole calculation is complex, and has many operations like this. I need as much