bigdecimal

losing precision converting from java BigDecimal to double

删除回忆录丶 提交于 2019-11-27 15:04:53
问题 I am working with an application that is based entirely on doubles, and am having trouble in one utility method that parses a string into a double. I've found a fix where using BigDecimal for the conversion solves the issue, but raises another problem when I go to convert the BigDecimal back to a double: I'm losing several places of precision. For example: import java.math.BigDecimal; import java.text.DecimalFormat; public class test { public static void main(String [] args){ String num =

Java BigDecimal memory usage?

99封情书 提交于 2019-11-27 14:59:57
Is there a guideline for estimating the amount of memory consumed by a BigDecimal ? Looking for something similar to these guidelines for estimating String memory usage. M. Jessup If you look at the fields in the source for BigDecimal there is: BigDecimal: long intCompact +8 bytes int precision +4 bytes int scale +4 bytes String stringCache +? BigInteger intVal +? BigInteger: int bitCount +4 bytes int bitLength +4 bytes int firstNonzeroIntNum +4 bytes int lowestSetBit +4 bytes int signum +4 bytes int[] mag +? The comment for stringCache says Used to store the canonical string representation,

Why does new BigDecimal(“0.0”).stripTrailingZeros() have a scale of 1?

老子叫甜甜 提交于 2019-11-27 14:40:39
Running this simple program: public static void main(final String... args) { System.out.println(BigDecimal.ZERO.scale()); System.out.println(new BigDecimal("0").scale()); System.out.println(new BigDecimal("0.0").stripTrailingZeros().scale()); System.out.println(new BigDecimal("1.0").stripTrailingZeros().scale()); } outputs: 0 0 1 0 My question is rather simple: why doesn't the third println output 0 ? That would seem logical... EDIT : OK, so, this is a very old bug: Bug Link and in fact, it "works" for any number of zeroes: new BigDecimal("0.0000").stripTrailingZeroes().scale() is 4! In fact

Why are my BigDecimal objects initialized with unexpected rounding errors?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 14:35:27
问题 In Ruby 2.2.0, why does: BigDecimal.new(34.13985572755337, 9) equal 34.0 but BigDecimal.new(34.13985572755338, 9) equal 34.1398557 ? Note that I am running this on a 64 bit machine. 回答1: Initialize with Strings Instead of Floats In general, you can't get reliable behavior with Floats. You're making the mistake of initializing your BigDecimals with Float values instead of String values, which introduces some imprecision right at the beginning. For example, on my 64-bit system: float1 = 34

What variable type can I use to hold huge numbers (30+ digits) in java?

断了今生、忘了曾经 提交于 2019-11-27 13:00:45
问题 Is there a really large variable type I can use in Java to store huge numbers (up to around forty digits)? long 's maximum value is 9223372036854775807, which is 19 digits -- not nearly large enough. I'm trying to create a calculator that can handle large numbers, because most nowadays can only hold an insufficient 10 digits or so, and I want want accurate calculations with numbers of a much larger magnitude EDIT Thanks for the answers. I can use BigInteger for big integers, the only limit

Ruby BigDecimal sanity check (floating point newb)

偶尔善良 提交于 2019-11-27 12:46:56
Is my understanding correct that with Ruby BigDecimal types (even with varying precision and scale lengths) should calculate accurately or should I anticipate floating point shenanigans? All my values within a Rails application are BigDecimal type and I'm seeing some errors (they do have different decimal lengths), hoping it's just my methods and not my object types. There are two common pitfalls when working with floating point arithmetic. The first problem is that Ruby floating points have fixed precision. In practice this will either be 1) no problem for you or 2) disastrous, or 3)

Equals operator for zeros (BigDecimal / Double) in Java

為{幸葍}努か 提交于 2019-11-27 12:45:34
问题 A few interesting observations w.r.t equals operator on 0 and 0.0 new Double(0.0).equals(0) returns false, while new Double(0.0).equals(0.0) returns true. BigDecimal.ZERO.equals(BigDecimal.valueOf(0.0)) returns false, while BigDecimal.ZERO.equals(BigDecimal.valueOf(0)) returns true. Looks like the string comparison is being done in both the cases. Could anyone throw some light on this. Thanks. 回答1: BigDecimal 'equals' compares the value and the scale. If you only want to compare values (0 ==

你真的会数钱吗?

一曲冷凌霜 提交于 2019-11-27 11:45:06
本文已迁移至: http://thinkinside.tk/2013/01/01/money.html 快年底了,假如你们公司的美国总部给每个人发了一笔201212.21美元的特别奖金,作为程序员的你, 该如何把这笔钱收入囊中? Table of Contents 1 美元?美元! 2 存入账户 3 收税 4 转成人民币 5 分钱 6 记账 7 来点高级的 8 其他未尽事宜 9 小结 1 美元?美元! 你可能觉得,这根本不是问题。在自己的账户中直接加上一笔“转入”就行了。但是首先就遇到了币种的问题。 一般来说,银行账户都是单币种的。你可能会说不对啊,我的一卡通就能存入不同的币种啊?但那是一个“账号(Account Number)”对应的多个“账户(Account)”。 通常财务记账的时候,一个“账户(Account)”都使用同一币种。 账户(Account)记录了资金的往来,包含很多条目(Entry)。账户会记录结余,结余等于所有条目中金额的总和。 我们不可能为每个币种设计一种条目,所以需要抽象出一个货币类——Money,适用于各种不同的币种: Money类至少要记录金额和币种: 对于金额,由于货币存在最小面额,所以金额的类型可以采用定点小数或者整型。考虑到会对金额进行一些运算,用整数处理应该更方便。如果用java语言实现,可以使用 lang类型。 对于币种,java提供了java

BigDecimal to string

爷,独闯天下 提交于 2019-11-27 11:02:58
问题 I have a BigDecimal object and i want to convert it to string. The problem is that my value got fraction and i get a huge number (in length) and i only need the original number in string for example: for BigDecimal bd = new BigDecimal(10.0001) System.out.println(bd.toString()); System.out.println(bd.toPlainString()); the output is: 10.000099999999999766941982670687139034271240234375 10.000099999999999766941982670687139034271240234375 and i need the out put to be exactly the number 10.0001 in

BigDecimal setScale and round

安稳与你 提交于 2019-11-27 10:21:43
What is the difference between this two call? (Is there any?) // 1. new BigDecimal("3.53456").round(new MathContext(4, RoundingMode.HALF_UP)); // 2. new BigDecimal("3.53456").setScale(4, RoundingMode.HALF_UP); dale peters One important point that is alluded to but not directly addressed is the difference between "precision" and "scale" and how they are used in the two statements. "precision" is the total number of significant digits in a number. "scale" is the number of digits to the right of the decimal point. The MathContext constructor only accepts precision and RoundingMode as arguments,