bigdecimal

Which method will be better when multiply 100 with BigDecimal?

泪湿孤枕 提交于 2019-12-06 02:58:06
问题 Here I have a question when using Java BigDecimal. Which way will be better when I want to multiply 100 for a object of BigDecimal. multiply 10 twice; movePointRight(2); scaleByPowerOfTen(2); any other way? please show me if there is. BTW, it will be used for commercial calculation, so I'm considering the precision rather than the speed. 回答1: Option 3 is the fastest. Options 1 and 2 are roughly the same (option one being to multiply by ten twice). Multiplying by 100 instead gets up near the

Get all decimal places in a number after division

白昼怎懂夜的黑 提交于 2019-12-05 23:18:34
问题 I am currently using the BigDecimal and it is giving me more decimals but not nearly enough for what I am trying to do. I need to be able to get all the way to the 10^6 digit. This is my current code BigDecimal num = new BigDecimal(103993/33102.0); pw.println(num.toString()); and it outputs 3.14159265301190249175533608649857342243194580078125 where the number actually has a lot more decimals: http://www.wolframalpha.com/input/?i=103993%2F33102 回答1: You are loosing the precision when

How can I compare a BigDecimal with ActiveRecord decimal field?

拜拜、爱过 提交于 2019-12-05 21:03:13
Assume a schema like this: create_table "bills", :force => true do |t| t.decimal "cost", :precision => 10, :scale => 5 end I want to write a function that writes a new bill to the DB iff it is unique. The following does not work: def load_bill_unless_exists(candidate) incumbents = Bill.scoped.where(:cost => candidate.cost) candidate.save unless incumbents.exists? end because the incumbent bills and the candidate bills have different limits in their BigDecimal representation, so the :cost => candidate.cost test fails. That is, it's comparing: candidate: #<Bill id: nil, cost: #<BigDecimal

BigDecimal 的用法

不问归期 提交于 2019-12-05 19:33:53
1.初始化 BigDecimal discount=new BigDecimal(0.9); BigDecimal discount=new BigDecimal(200); 2.加减乘除 加法 add()函数 减法subtract()函数 乘法multiply()函数 除法divide()函数 绝对值abs()函数 如: BigDecimal result1 = num1.add(num2); //减法 BigDecimal result2 = num1.subtract(num2); //乘法 BigDecimal result3 = num1.multiply(num2); //绝对值 BigDecimal result4 = num3.abs(); //除法 BigDecimal result5 = num2.divide(num1,20,BigDecimal.ROUND_HALF_UP); ------------------ public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) scale:要精确的小数位数 roundingMode:舍入模式 有八种模式,ROUND_HALF_UP(四舍五入模式) 3.比较大小 BigDecimal a = new BigDecimal

hessian-4.0.7不能正确传输BigDecimal类型数字

时光怂恿深爱的人放手 提交于 2019-12-05 19:08:24
前些时候开发中,使用的是hessian,在返回实体类的时候,发现每次返回到前端的bigDeciaml类型数字都是0,百思不得其解啊,各种调试,都未成功。后来发现是hessian在传输BigDecimal数字时出现了问题。 后来发现是hessian-4.0.7.jar中未指定BigDecimal类的序列化类,不过在hessian-4.0.37版本中发现有修复的版本,可以直接下载4.0.37使用,不过也有别的解决方案: 1、 增加下面两个文件,打入hessian包。 /META-INF/hessian/serializers,内容如下: java.math.BigDecimal=com.caucho.hessian.io.BigDecimalDeserializer /META-INF/hessian/deserializers java.math.BigDecimal=com.caucho.hessian.io.StringValueSerializer 2:、在传输的时候转换一下,可以发送的时候转成String类型,然后在接收的时候在利用new BigDecimal()转换成BigDecimal类型,不过要注意一个空指针问题。 来源: oschina 链接: https://my.oschina.net/u/1034481/blog/535341

How to convert java BigDecimal to normal byte array (not 2's complement)

拥有回忆 提交于 2019-12-05 16:46:41
How do I convert from big integer to a byte array which is not in 2's complement format. Bascially I only need to convert positive numbers and do not need the sign bit. So something like 10 would become a byte 0x0a i.e-> 00001010 [Update] As per comment I tried this public void testBinary() { BigDecimal test = new BigDecimal(35116031); BigInteger theInt = test.unscaledValue(); byte[] arr = theInt.toByteArray(); System.out.println(getCounterVal(arr, new BigInteger("256"))); } public BigInteger getCounterVal(byte[] arr, BigInteger multiplier) { BigInteger counter = BigInteger.ZERO; for(int i =

Maximum number of digits after the decimal point using BigDecimal

筅森魡賤 提交于 2019-12-05 14:31:59
问题 What is the maximum number of digits we can have after the decimal point of a BigDecimal value in Java? 回答1: 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

BigDecimal

坚强是说给别人听的谎言 提交于 2019-12-05 14:25:38
public class BigDecimalUtils { /** * 默认除法运算精度 */ private static final int DEFAULT_DIV_SCALE = 8; /** * 采用 BigDecimal 的字符串构造器进行初始化。 * * @param v double 值 * @return BigDecimal 对象 */ private static BigDecimal createBigDecimal(double v) { return new BigDecimal(Double.toString(v)); } /** * 提供精确的加法运算。 * * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public static BigDecimal add(BigDecimal v1, BigDecimal v2) { return v1.add(v2); } /** * 提供精确的加法运算。 * * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public static BigDecimal add(double v1, double v2) { BigDecimal b1 = createBigDecimal(v1);

What is the difference between BigDecimal movePointRight and scaleByPowerOfTen?

蓝咒 提交于 2019-12-05 12:37:27
问题 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? 回答1: movePointRight will prevent a negative scale from occurring if it results

Scala and Java BigDecimal

为君一笑 提交于 2019-12-05 08:54:45
问题 I want to switch from Java to a scripting language for the Math based modules in my app. This is due to the readability, and functional limitations of mathy Java. For e.g, in Java I have this: BigDecimal x = new BigDecimal("1.1"); BigDecimal y = new BigDecimal("1.1"); BigDecimal z = x.multiply(y.exp(new BigDecimal("2")); As you can see, without BigDecimal operator overloading, simple formulas get complicated real quick. With doubles, this looks fine, but I need the precision. I was hoping in