bigdecimal

How do I check if a BigDecimal is in a Set or Map in a scale independent way?

心不动则不痛 提交于 2019-12-01 14:27:56
问题 BigDecimal's equals() method compares scale too, so new BigDecimal("0.2").equals(new BigDecimal("0.20")) // false It's contested why it behaves like that. Now, suppose I have a Set<BigDecimal> , how do I check if 0.2 is in that Set, scale independent? Set<BigDecimal> set = new HashSet<>(); set.add(new BigDecimal("0.20")); ... if (set.contains(new BigDecimal("0.2")) { // Returns false, but should return true ... } 回答1: contains() will work as you want it to if you switch your HashSet to a

Remove the leading 0's till the decimal

一曲冷凌霜 提交于 2019-12-01 13:10:57
问题 I want to remove the leading zeros in the decimal numbers. So i want the output should be .324 not 0.324 . I tried str.replaceFirst("^0+(?!$)", ""); Didn't work and i also tried the regex! No results! And yes I am working with BigDecimal. 回答1: Try this str = str.replaceFirst("^0\\.", "."); 回答2: if you are trying to format BigDecimal having value 0.324 to .324 . Then below works BigDecimal bd=new BigDecimal("0.23");// this will replace with your BigDecimal DecimalFormat df=null; //check if

Converting String to BigDecimal in GWT

风格不统一 提交于 2019-12-01 10:48:36
In my GWT web application I have a textbox that holds a price. How can one convert that String to a BigDecimal? The easiest way is to create new text box widget that inherits ValueBox. If you do it this way, you won't have to convert any string values manually. the ValueBox takes care of it all. To get the BigDecimal value entered you can just go: BigDecimal value = myTextBox.getValue(); Your BigDecimalBox.java : public class BigDecimalBox extends ValueBox<BigDecimal> { public BigDecimalBox() { super(Document.get().createTextInputElement(), BigDecimalRenderer.instance(), BigDecimalParser

BigDecimal类的加/减/乘/除

我与影子孤独终老i 提交于 2019-12-01 07:57:31
对于不需要任何准确计算精度的数字可以直接使用float或double,但是如果需要精确计算的结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数的操作。BigDecimal类的常用方法如表11-15所示。 表11-15 BigDecimal类的常用方法 序号 方 法 类型 描 述 1 public BigDecimal(double val) 构造 将 double 表示形式转换 为 BigDecimal 2 public BigDecimal(int val) 构造 将 int 表示形式转换为 BigDecimal 3 public BigDecimal(String val) 构造 将字符串表示 形式转换为 BigDecimal 4 public BigDecimal add(BigDecimal augend) 普通 加法 5 public BigDecimal subtract(BigDecimal subtrahend) 普通 减法 6 public BigDecimal multiply(BigDecimal multiplicand) 普通 乘法 7 public BigDecimal divide(BigDecimal divisor) 普通 除法 范例:进行四舍五入的四则运算 来源: oschina 链接: https://my

在商业计算中我们要用java.math.BigDecimal

99封情书 提交于 2019-12-01 07:57:20
1.引言 借用《Effactive Java》这本书中的话,float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算,这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而,它们没有提供完全精确的结果,所以不应该被用于要求精确结果的场合。但是,商业计算往往要求结果精确,这时候BigDecimal就派上大用场啦。 2.BigDecimal简介 BigDecimal 由任意精度的整数非标度值 和32 位的整数标度 (scale) 组成。如果为零或正数,则标度是小数点后的位数。如果为负数,则将该数的非标度值乘以 10 的负scale 次幂。因此,BigDecimal表示的数值是(unscaledValue × 10-scale)。 3.测试代码 3.1构造函数(主要测试参数类型为double和String的两个常用构造函数) BigDecimal aDouble =new BigDecimal(1.22); System.out.println("construct with a double value: " + aDouble); BigDecimal aString = new BigDecimal("1.22"); System.out.println("construct with a String value: " +

Hibernate loss of precision in results when mapping a number (22,21) to BigDecimal

眉间皱痕 提交于 2019-12-01 06:01:25
I have this column in my Oracle 11g mapped as NUMBER (21,20), which is mapped in Hibernate as: @Column(name = "PESO", precision = 21, scale = 20, nullable = false) public BigDecimal getWeight() { return weight; } For a particular record for which the value of the column is 0.493 I get a BigDecimal whose value is 0.49299999999. It seems that somewhere there is a loss of precision due (maybe) to a Double or Float conversion, but I couldn't track it down with a simple unit test like this: Double d = new Double("0.493"); System.out.println((d)); Any variant of that code, using Float, BigDecimal

How to get Java scanner to acknowledge blank input?

£可爱£侵袭症+ 提交于 2019-12-01 05:46:57
I am having trouble getting my program to respond to empty inputs. For example, say I wanted to prompt the user to enter a value for money with type BigDecimal as well as a currency type. Here is what the program in question looks like. public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the amount of money " + "and specify currency (USD or CNY): "); // initialize variable moneyInput BigDecimal moneyInput; // check if moneyInput is numeric try { moneyInput = input.nextBigDecimal(); } catch (InputMismatchException e){ System.err.println(

BigDecimal Error

和自甴很熟 提交于 2019-12-01 05:46:05
In Java, I have defined k as double k=0.0; I am taking data from database and adding the same using while loop, while(rst.next()) { k = k + Double.parseDouble(rst.getString(5)); } NOTE: In database, I have values as 125.23, 458.45, 665.99 (all two decimals) When I display k, I get value as k = 6034.299999999992 Hence I introduced BigDecimal and changed code to below BigDecimal bd = new BigDecimal(k); bd = bd.setScale(2,BigDecimal.ROUND_UP); Now I get new total as bd=6034.30 which is correct. Problem 1 Well the problem is when I am using same at other place, below is what I am getting k = 157.3

mybatis的批量update

只愿长相守 提交于 2019-12-01 05:04:26
方法有三种:1.通过java代码batch方式,xml文件只需一条update语句。java代码繁琐 2.xml使用foreach,“;”分割多条update语句,要求:jdbc的url需加上allowMultiQueries=true。速度慢 3.xml使用foreach进行代码拼接,用了case...when...then...end,oracle好像不支持这种写法,mysql和sqlserver是可以的 xml文件: <mapper namespace="com.dao.EmployeeDao"> <!--可以通过改变实体类的字段来验证映射--> <update id="updateFirstWay"> update employee set employee_name = #{employeeName}, employee_gender = #{employeeGender}, employee_salary = #{employeeSalary}, dept_id = #{deptId} where employee_id = #{employeeId} </update> <update id="updateSecondWay"> <foreach collection="list" item="emp" separator=";"> update employee

BCD math library for arbitrary big numbers?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 03:45:29
I'm looking for a replacement of the stock Delphi Data.FmtBcd library because I just hit its limits like maximum decimal digits it can represent and program terminates with EBcdOverflowException . For the curious, I'm calculating arithmetic series members and need to handle very large numbers - hundred-thousands positions are not so uncommon. And also get results in a reasonable time. I did rewritten part of the code to Python 3.2 for the testing purposes and calculation speed would be sufficient for the Delphi's equivalent. Some recommendations for a such library, preferably free or