bigdecimal

BigDecimal in 1.8 vs. 1.9

不想你离开。 提交于 2019-12-04 08:46:00
When Upgrading to ruby 1.9, I have a failing test when comparing expected vs. actual values for a BigDecimal that is the result of dividing a Float. expected: '0.495E0',9(18) got: '0.4950000000 0000005E0',18(27) googling for things like "bigdecimal ruby precision" and "bigdecimal changes ruby 1.9" isn't getting me anywhere. How did BigDecimal 's behavior change in ruby 1.9? update 1 > RUBY_VERSION => "1.8.7" > 1.23.to_d => #<BigDecimal:1034630a8,'0.123E1',18(18)> > RUBY_VERSION => "1.9.3" > 1.23.to_d => #<BigDecimal:1029f3988,'0.123E1',18(45)> What does 18(18) and 18(45) mean? Precision I

Oracle hibernate sequence generator problem

扶醉桌前 提交于 2019-12-04 08:02:26
问题 I am developing an application using oracle 11g, Java(struts2) and Hibernate. I have table named mytemp with column mytemp_id which is of type NUMBER(22,0). In my mytemp.hbm.xml file id is as given below <id name="mytempId" type="big_decimal"> <column name="MYTEMP_ID" precision="22" scale="0" /> <generator class="sequence"> <param name="sequence">MYTEMP_TEMP_ID_SEQ</param> </generator> </id> In my Oracle database sequence named "MYTEMP_TEMP_ID_SEQ" is created and working fine in Oracle. Now

How to convert decimal timestamp to date in Java with trailing decimals

梦想的初衷 提交于 2019-12-04 05:46:01
问题 I have been trying to figure out how to convert a timestamp to a date but with the trailing decimals at the end, so for example: Timestamp - C50204EC EC42EE92 is equivalent to Sep 27, 2004 03:18:04.922896299 UTC. The timestamp format includes the first 32-bit unsigned seconds as a field spanning 136 years and the 32-bit fraction field resolving 232 picoseconds. In the timestamp formats, the prime epoch, or base date of era 0, is 0 h 1 January 1900 UTC, when all bits are zero. This is what I

策略模式-设计模式

跟風遠走 提交于 2019-12-04 04:07:47
在讲述之前,我们首先看小例子: 现实生活中我们去商场上买东西的时候,卖场经常根据不同的客户来制定不同的报价策略,比如新客户不打折扣,针对老客户打9折,针对VIP打8折…… 现在我们做一个报价管理模块,简要点就是针对不同的客户,提供不同的报价。 假如是有你来做,你会怎么做? 在日常的开发中,我们大部分会写出如下的代码片段: public class QuoteManager { public BigDecimal quote(BigDecimal originalPrice,String customType){ if ("新客户".equals(customType)) { System.out.println("抱歉!新客户没有折扣!"); return originalPrice; }else if ("老客户".equals(customType)) { System.out.println("恭喜你!老客户打9折!"); originalPrice = originalPrice.multiply(new BigDecimal(0.9)).setScale(2,BigDecimal.ROUND_HALF_UP); return originalPrice; }else if("VIP客户".equals(customType)){ System.out.println(

What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java?

你说的曾经没有我的故事 提交于 2019-12-04 03:33:12
I have to integrate my web application with a payment gateway. I want to input total amount in USD and then convert it into Cents as my payment gateway library accepts amount in Cents (of type Integer ). I found that Big Decimal in java is best way for manipulation of currency. Currently I take input as say USD 50 and convert it to Integer like this: BigDecimal rounded = amount.setScale(2, BigDecimal.ROUND_CEILING); BigDecimal bigDecimalInCents = rounded.multiply(new BigDecimal("100.00")); Integer amountInCents = bigDecimalInCents.intValue(); Is this the correct way of converting USD to Cents

BigDecimal Subtraction

元气小坏坏 提交于 2019-12-04 03:27:54
I want to substract 2 double values, and I have tried the following code. double val1 = 2.0; double val2 = 1.10; System.out.println(val1 - val2); and I got the output as, 0.8999999999999999 For getting output as 0.9 I tried with BigDecimal as follows, BigDecimal val1BD = new BigDecimal(val1); BigDecimal val2BD = new BigDecimal(val2); System.out.println(val1BD.subtract(val2BD)); And I got the output as, 0.899999999999999911182158029987476766109466552734375 Then I tried with BigDecimal.valueOf() val1BD = BigDecimal.valueOf(val1); val2BD = BigDecimal.valueOf(val2); System.out.println(val1BD

BigDecimal precision not persisted with JPA annotations

限于喜欢 提交于 2019-12-04 03:25:05
I am using the javax.persistence API and Hibernate to create annotations and persist entities and their attributes in an Oracle 11g Express database. I have the following attribute in an entity: @Column(precision = 12, scale = 9) private BigDecimal weightedScore; The goal is to persist a decimal value with a maximum of 12 digits and a maximum of 9 of those digits to the right of the decimal place. After calculating weightedScore , the result is 0.1234, but once I commit the entity with the Oracle database, the value displays as 0.12. I can see this by either using an EntityManager object to

Maximum number of digits after the decimal point using BigDecimal

╄→гoц情女王★ 提交于 2019-12-04 02:22:19
What is the maximum number of digits we can have after the decimal point of a BigDecimal value in Java? It's (almost) unlimited. You can store roughly 2 billion digits after the decimal point if scale is set to the maximum value of an integer, although you may run out of memory if you try to do this. If you need to store so many digits that the limit is a problem then you probably need to rethink the design of your program. See the BigDecimal documentation : Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32

What is the difference between BigDecimal movePointRight and scaleByPowerOfTen?

不问归期 提交于 2019-12-04 01:36:49
With the following code: BigDecimal x = new BigDecimal("34.5678"); BigDecimal a = x.movePointRight(3); BigDecimal b = x.scaleByPowerOfTen(3); BigDecimal c = x.movePointRight(-3); BigDecimal d = x.scaleByPowerOfTen(-3); a and b are both 34567.8 and c and d are both 0.0345678. a.scale() and b.scale are both 1 and c.scale() and d.scale() are both 7. In what circumstances do these two methods produce different results? movePointRight will prevent a negative scale from occurring if it results in one. scaleByPowerOfTen will not prevent this. Example code: import java.math.BigDecimal; public class

BigDecimal

不羁岁月 提交于 2019-12-03 22:54:57
一、 计算机的小数计算一定范围内精确,超过范围只能取近似值: 计算机存储的浮点数受存储bit位数影响,只能保证一定范围内精准,超过bit范围的只能取近似值。 java中各类型的精度范围参见:http://blog.csdn.net/longshenlmj/article/details/47616481 编程时注意: doulbe类型的数,不能用等号判定是否相等(或者是一定范围内可以)。因为两次同样的计算(除法)结果可能出现小数部分不同。甚至极端的时候,初始化两个小数时,都可能不相等(用数值和字符串分别初始化bigdecimal的小数就会不等) java小数处理方法的经验总结: (1)小数计算对精度无要求时,使用float节省时间。 (2)如果有精度要求,用BigDecimal类处理(初始化必须使用字符串,因为用数值初始化会得到近似值,不准确),然后设置保留位数和 舍入法(half_up四舍五入,half_even银行家,half_down向下取整) (3)精度要求低时可转化为整数处理(集体统一扩大数量级): 乘以10的级数转化为整数处理,小数点右移几位,但整数值不要超过对应类型的取值范围。比如保留4位小数,可统一乘以10000,然后只保留整数计算结果,保留近位的话就多乘一位。 这种方式在RTB项目MDSP的算法核心模块中使用,几十万的投放量,用int或long就可以处理