bigdecimal

Java BigDecimal without E

我怕爱的太早我们不能终老 提交于 2019-12-01 03:12:15
I have a BigDecimal variable BigDecimal x = new BigDecimal("5521.0000000001"); Formula: x = x.add(new BigDecimal("-1") .multiply(x.divideToIntegralValue(new BigDecimal("1.0")))); I want to remove the integer part, to get the value x = ("0.0000000001") , but my new value is 1E-10 and not the 0.0000000001. To get a String representation of the BigDecimal without the exponent part, you can use BigDecimal.toPlainString() . In your example: BigDecimal x = new BigDecimal("5521.0000000001"); x = x.add(new BigDecimal("-1"). multiply(x.divideToIntegralValue(new BigDecimal("1.0")))); System.out.println

BigDecimal运算

有些话、适合烂在心里 提交于 2019-12-01 02:56:54
在写老师留的作业的时候( 1. 使用for循环计算1+1/2!+1/3!+…+1/20! )本来想用double来直接计算的,但是突然发现精度不够,于是现学了一波BigDecimal,(发现BigDecimal主要用于商业用途em....只做个作业的我应该之后会很少接触了,大概) 这里加个普及吧:Java中float的精度为6-7位有效数字。double的精度为15-16位 一开始就从最基础的初始化开始吧: 我这里就用int,double和string进行转换,结果如下 除了double有些问题外其他的都很正常,先不急,之后我们来看一下运算之后的数值, 现在问题就有些大了,因为double精度的缺失有些严重,所以在进行运算的时候的精度的不准确性就越来越大,至于为什么会这样,就先归功于double的构造方法的结果有一定的不可预知性,而String 构造方法是完全可预知的,但是在正常情况下都会要用double类型的,所以非要用double类型来构造的话,可以使用 toString(double)转换 String,然后 使用String构造方法,或使用BigDecimal的静态方法valueOf,如下图: 或者可以写一个工具类。 接下来就是运算——加,减,乘,除,绝对值: 这里特别强调一下除法,因为不能出现整除的情况可能会报错,所以这个时候我看见除法可以有三个参数,所以就上网查了一下

Can you make a TextField<BigDecimal> accept both , and . as decimal separator?

喜夏-厌秋 提交于 2019-12-01 01:39:52
问题 In a Wicket app, I have a decimal number text field: TextField<BigDecimal> f = new TextField<BigDecimal>("f", new PropertyModel<BigDecimal>(model, "share")); I want it to always accept both . (dot) and , (comma) as decimal separator ( regardless of browser's locale settings). For showing the value, session's locale is used [which in our case is forced to be "fi" (-> comma)], but here I'm interested in what the field accepts as input. My question is, do I have to change the field to TextField

Java中BigDecimal类介绍及用法

∥☆過路亽.° 提交于 2019-11-30 22:04:42
Java中提供了大数字(超过16位有效位)的操作类,即 java.math.BinInteger 类和 java.math.BigDecimal 类,用于高精度计算.    其中 BigInteger 类是针对大整数的处理类,而 BigDecimal 类则是针对大小数的处理类.   BigDecimal 类的实现用到了 BigInteger类,不同的是 BigDecimal 加入了小数的概念.   float和Double只能用来做科学计算或者是工程计算;在商业计算中,对数字精度要求较高,必须使用 BigInteger 类和 BigDecimal 类,它支持任何精度的定点数,可以用它来精确计算货币值.    BigDecimal类创建的是对象,不能使用传统的+、-、*、/等算术运算符直接对其进行数学运算,而必须调用其对应的方法.方法的参数也必须是BigDecimal类型的对象. 一、构造BigDecimal 对象常用方法   1、方法一 BigDecimal BigDecimal(double d); //不允许使用    2、方法二 BigDecimal BigDecimal(String s); //常用,推荐使用    3、方法三 static BigDecimal valueOf(double d); //常用,推荐使用    注意:    1. double

java常用类 比较器/system/math/big

醉酒当歌 提交于 2019-11-30 21:37:02
Java 比较器   自然排序:java.lang.Comparable   定制排序:java.util.Comparator 自然排序:java.lang.Comparable    Comparable接口强行对实现它的每个类的对象进行整体排序。这种排序被称 为类的自然排序。    实现 Comparable 的类必须实现 compareTo(Object obj) 方法,两个对象即 通过 compareTo(Object obj) 方法的返回值来比较大小。如果当前对象this大 于形参对象obj,则返回正整数,如果当前对象this小于形参对象obj,则返回 负整数,如果当前对象this等于形参对象obj,则返回零。    实现Comparable接口的对象列表(和数组)可以通过 Collections.sort 或 Arrays.sort进行自动排序。实现此接口的对象可以用作有序映射中的键或有 序集合中的元素,无需指定比较器。    对于类 C 的每一个 e1 和 e2 来说,当且仅当 e1.compareTo(e2) == 0 与 e1.equals(e2) 具有相同的 boolean 值时,类 C 的自然排序才叫做与 equals 一致。建议(虽然不是必需的)最好使自然排序与 equals 一致。    Comparable 的典型实现:(默认都是从小到大排列的)  

How can I create a random BigDecimal in Java?

一曲冷凌霜 提交于 2019-11-30 21:21:51
This question: How to generate a random BigInteger describes a way to achieve the same semantics as Random.nextInt(int n) for BigIntegers. I would like to do the same for BigDecimal and Random.nextDouble(). One answer in the above question suggests creating a random BigInteger and then creating a BigDouble from it with a random scale. A very quick experiment shows this to be a very bad idea :) My intuition is that using this method would require the integer to be scaled by something like n-log10(R) , where n is the number of digits of precision required in the output and R is the random

Formatting a String to Remove Scientific Notation - Java

只谈情不闲聊 提交于 2019-11-30 20:26:17
I have the following code which splits a string of numbers (separated by a space) and then creates an array of floats : //Split the string and then build a float array with the values. String[] tabOfFloatString = value.split(" "); int length = tabOfFloatString.length; System.out.println("Length of float string is" + length); float[] floatsArray = new float[length]; for (int l=0; l<length; l++) { float res = Float.parseFloat(tabOfFloatString[l]); System.out.println("Float is " + res); floatsArray[l]=res; } The problem is that some of the values in the string are formatted with scientific

java基础(17):包装类、System、Math、Arrays、大数据运算

情到浓时终转凉″ 提交于 2019-11-30 20:00:59
1. 基本类型包装类 大家回想下,在第三篇文章中我们学习Java中的基本数据类型时,说Java中有8种基本的数据类型,可是这些数据是基本数据,想对其进行复杂操作,变的很难。怎么办呢? 1.1 基本类型包装类概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的。而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型,考试成绩需要转换成double类型等。那么,想实现字符串与基本数据之间转换怎么办呢? Java中提供了相应的对象来解决该问题,基本数据类型对象包装类:java将基本数据类型值封装成了对象。封装成对象有什么好处?可以提供更多的操作基本数值的功能。 8种基本类型对应的包装类如下: 其中需要注意int对应的是Integer,char对应的Character,其他6个都是基本类型首字母大写即可。 基本数据类型对象包装类特点:用于在基本数据和字符串之间进行转换。 将字符串转成基本类型: parseXXX(String s);其中XXX表示基本类型,参数为可以转成基本类型的字符串,如果字符串无法转成基本类型,将会发生数字转换的问题 NumberFormatException System.out.println(Integer.parseInt("123") + 2); //打印结果为 125

Is Java's BigDecimal the closest data type corresponding to C#'s Decimal?

妖精的绣舞 提交于 2019-11-30 18:23:57
According to the chart here , the equivalent data type in Java to C#'s Decimal is BigDecimal . Is this really so? What's up with the "Big" preamble? There doesn't seem to be a "SmallDecimal" or "LittleDecimal" (let alone "MediumSizedDecimal") in Java. I must say, though, that chart was the clearest thing I found on the subject; the other links here and here and here were about as clear to me as the Mississippi River after a torrential tempest. Yep - that's the corresponding type. Since you are using Java after C# - don't be too surprised to find little nuances like this - or be too upset when

BigDecimal compareTo not working as expected

我只是一个虾纸丫 提交于 2019-11-30 17:36:39
According to the JavaDoc for BigDecimal , the compareTo function does not account for the scale during comparison. Now I have a test case that looks something like this: BigDecimal result = callSomeService(foo); assertTrue(result.compareTo(new BigDecimal(0.7)) == 0); //this does not work assertTrue(result.equals(new BigDecimal(0.7).setScale(10, BigDecimal.ROUND_HALF_UP))); //this works The value I'm expecting the function to return is 0.7 and has a scale of 10. Printing the value shows me the expected result. But the compareTo() function doesn't seem to be working the way I think it should.