{"id":7278,"date":"2014-06-03T23:30:34","date_gmt":"2014-06-03T23:30:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/problem-about-bigdecimal-collection-of-common-programming-errors\/"},"modified":"2014-06-03T23:30:34","modified_gmt":"2014-06-03T23:30:34","slug":"problem-about-bigdecimal-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/06\/03\/problem-about-bigdecimal-collection-of-common-programming-errors\/","title":{"rendered":"problem about bigdecimal-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/71770d043c0f7e3c7bc5f74190015c26?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nthe Tin Man<br \/>\nrubygems bundle bigdecimal gemfile<br \/>\nI&#8217;m using Bundle to install gems in d:\/cucumbertests\/cucumber\/ successfully, which installs BigDecimal v 1.1.0. This failed in d:\/cucumbertests\/cucumber\/itinfix which installed BigDecimal v 1.2.0.How do I identify the correct version of BigDecimal in my Gemfile in d:\/cucumbertests\/cucumber\/itinfix? Why is the version of BigDecimal correct in d:\/cucumbertests\/cucumber? The latest version is 1.2.0, found by running gem query &#8211;remote &#8211;name-matches bigdecimal is:REMOTE GEMS:bigdecimal (1.2.0) bigd<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/84a4143803eafa1966191c604864a7b5?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nDavid James<br \/>\njava bigdecimal floating-point-precision<br \/>\nI have an XSD that requires me to use a BigDecimal for a lat\/lon. I currently have the lat\/lon as doubles, and convert them to BigDecimal, but I am only required to use about 12 places of precision. I have not been able to figure out how to set that. Can anyone help me with this?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f7ee84f1075068458826f0b1ad0f98e4?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nPazoozaTest Pazman<br \/>\njavascript bigdecimal<br \/>\nI am using bigDecimal.js:https:\/\/github.com\/iriscouch\/bigdecimal.jsin my node.js project server side with no problems however when I try the same on the client I can&#8217;t seem to reference the bigDecimal object correctly. On the github page it has a link: http:\/\/jhs.iriscouch.com\/demo\/_design\/bigdecimal\/_show\/ui?e=321.4567890*123456789.123456789987654321I have a link to bigdecimal.js in my html:&lt;script src=&#8221;\/javascripts\/bigdecimal.js&#8221; type=&#8221;text\/javascript&#8221;&gt;&lt;\/script&gt;and the script is d<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/KFhgw.jpg?s=32&amp;g=1\" \/><br \/>\nEric Leschinski<br \/>\njava double bigdecimal<br \/>\nIn Java, I want to take a double value and convert it to a BigDecimal and print out its String value to a certain precision. import java.math.BigDecimal; public class Main {public static void main(String[] args) {double d=-.00012;System.out.println(d+&#8221;&#8221;); \/\/This prints -1.2E-4double c=47.48000;BigDecimal b = new BigDecimal(c);System.out.println(b.toString()); \/\/This prints 47.47999999999999687361196265555918216705322265625 } }It prints this huge thing:47.4799999999999968736119626555591821670532<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/136adca04aede3c68157e66063d4a01f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1514499<br \/>\njava bigdecimal<br \/>\nI tried the following code. but getting different result when subtracting using BigDecimal. double d1 = 0.1;double d2 = 0.1;System.out.println(&#8220;double result: &#8220;+ (d2-d1));float f1 = 0.1F;float f2 = 0.1F;System.out.println(&#8220;float result: &#8220;+ (f2-f1));BigDecimal b1 = new BigDecimal(0.01);BigDecimal b2 = new BigDecimal(0.01);b1 = b1.subtract(b2);System.out.println(&#8220;BigDecimal result: &#8220;+ b1);Result: double result: 0.0 float result: 0.0 BigDecimal result: 0E-59I am still working on this. can anyone pl<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/322f2abf809f2b2fc4a51e54d4601f4a?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nJoJo<br \/>\njava bigdecimal<br \/>\nI have the following code in Java;if (selectPrice.compareTo(new BigDecimal(&#8220;0.00&#8221;)) == 0){return true;}What is the best way to write the IF condition?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/bca617231294691f95f0e9f0ee623630?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nRoman C<br \/>\njava bigdecimal round<br \/>\nPossible Duplicate:Addition for BigDecimal I feel like I&#8217;m missing something obvious, but cannot figure out what it is. I&#8217;m trying to use BigDecimal to get the decimal portion of a division between two numbers. A very simple case, 1.00 divided by 3.00 should be 0.(3) repeating. However, I keep getting a result of 1.00.BigDecimal num = new BigDecimal(1.00).setScale(2); BigDecimal divisor = new BigDecimal(3.00).setScale(2);num.divide(divisor, BigDecimal.ROUND_HALF_UP);System.out.print(num);My res<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/dc91a8e29fedbd6a6d7e03a8394009ab?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nNathan Hughes<br \/>\njava websphere nullpointerexception bigdecimal<br \/>\nRunning Java application on IBM WebSphere caused such exception:Caused by: java.lang.NullPointerException at java.math.BigDecimal.add2DFP(BigDecimal.java:1946) at java.math.BigDecimal.add(BigDecimal.java:1881) at com.somepackage.components.view.PremiumSummaryViewModel.setPremiums(PremiumSummaryViewModel.java:101)Null check is done. As I reviewed java.math.BigDecimal class has no such method add2DFP and does not call one either. Maybe it&#8217;s specific to IBM&#8217;s JDK.Any comment on this would be apprec<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/5f1a1f7d48c03f3e24d847ffb41b951c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nAprian<br \/>\njava android bigdecimal<br \/>\nI&#8217;m currently using BigDecimal and here is one of my implementation for getting a number with 2 precision:mDbHelper.open();BigDecimal total = mDbHelper.getTotal(Type.Income, &#8220;20130101 12:00:00&#8221;, &#8220;20131231 12:00:00&#8221;);BigDecimal expense = mDbHelper.getTotal(Type.Expense, &#8220;20130101 12:00:00&#8243;,&#8221;20131231 12:00:00&#8221;);BigDecimal saving = mDbHelper.getSaving(&#8220;feb&#8221;);mDbHelper.close();BigDecimal remaining = total.subtract(expense.add(saving));Log.d(&#8220;total get&#8221;, total.toPlainString());Log.d(&#8220;expense get&#8221;, ex<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/c140c722a4e9879d9b5761de6990db45?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nOleg Mikheev<br \/>\njava bigdecimal result<br \/>\nI&#8217;m running into a strange java bug using the intValue() from the BigDecimal class.I&#8217;m starting with a value of 3300028000.When I call the following code:int i = d.intValue();it returns: -994939296Any ides as to why this would happen?<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/98a2b2fb7ba8e1c1fa8e7bff95d523de?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nX.L.Ant<br \/>\njava exception bigdecimal exponent<br \/>\nthis is my first question asked in this forum, so if question is novice please advice.I receive the user input through EditText, hence it is a charsequence\/string. then i created BigDecimal to hold that value. it handles exponents also gracefully without any trouble. but problem is when user enters values like 1.1E+ (without any trailing exponent value), code crashes in this piece of code. but it works when input is like this 1.1E+2EditText edit_text_left; Editable editable_val_text; &#8230;. editab<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7e7a1accbdba46d580172b43527ed201?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nmtk<br \/>\njava bigdecimal<br \/>\nI using the following code and i added the support for big decimal and the compiler show error on the create object for the big decimal new BigDecimal(nextRandom),how can I overcome it?All the other types are working as expected.public static SwitchInputType&lt;?&gt; switchInput(final String typeName, final String memberName, final int cnt, boolean random) { &#8230; } else if (typeName.equals(&#8220;decimal&#8221;) || (typeName.equals(&#8220;java.math.BigDecimal&#8221;))) {BigDecimal nextRandom = RandomizeValues.nextRandom<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1352b07a60f3d7a90b981d12dab4849f?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nblawzoo<br \/>\nruby-on-rails bigdecimal<br \/>\nWhy do I get nil can&#8217;t be coerced into BigDecimal when I try to perform a calculation: here&#8217;s the code: model\/drink.rbclass Drink &lt; ActiveRecord::Basebelongs_to :menu before_save :total_amount def total_amountself.total_amount = self.price * self.quantity end model\/menu.rbclass Menu &lt; ActiveRecord::Basehas_many :drinks, :dependent =&gt; :destroyaccepts_nested_attributes_for :drinks, :allow_destroy =&gt; true#Validationsend*Drink is the (nested)child model and Menu the parent model When I a<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/66f153f22064414a5f50c2f5f376dc69?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nApples<br \/>\ngwt bigdecimal<br \/>\nI am trying to use the stripTrailingZeros() method of BigDecimal in GWT but it looks as if it is not supported by the JRE emulator. In the GWT dev mode console I get the following:[ERROR] Errors in file&#8230; LINE 585: The method stripTrailingZeros() is undefined for type BigDecimalI am using GWT 2.0.4. Does anybody else have this issue.<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/e4b24d290836076bb32b6c55968f29bd?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nScott Severance<br \/>\njavascript bigdecimal<br \/>\nMain questionI&#8217;m using this BigDecimal, and I&#8217;m trying to create a new MathContext object to pass to BigDecimal&#8217;s divide() method. But everything I&#8217;ve tried throws exceptions saying that it&#8217;s undefined. Here are some examples of things I&#8217;ve tried that don&#8217;t work:context = new MathContext(); \/\/ ReferenceError: MathContext is not defined context = new BigDecimal.MathContext(); \/\/ TypeError: undefined is not a function context = new BigDecimal.prototype.Ma<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/7a2e1a6c541819e636ad1434b84e7bee?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nn000b<br \/>\njava algorithm bigdecimal<br \/>\nThis is a problem from CodeSprint3 https:\/\/cs3.interviewstreet.com\/challenges\/dashboard\/#problem\/50877a587c389 Basically the problem is to calculate the number of possible combinations,nCr for given n and r.Also, 1 &lt;= n &lt;= 1000000000 and 0 &lt;= r &lt;= n. Output all answers modulo 142857.Since 6C4=6!\/4! 2!=6*5\/2! =6*5\/2*1I thought overflow could be avoided using division at every step.That is to start with value of n (n is 6 in this case). Decrement n and multiply it with previous va<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/1f9ebc17450d8abc1e4e00c0e07fa133?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nuser1707455<br \/>\njava biginteger bigdecimal<br \/>\nI am trying to take a text file with fractions on it. Convert the fractions from strings to bigdecimal. Then divide and store the results on a stack. Each line in the txt file is one fraction which was randomly generated with the char &#8216;\/&#8217; separating the numerator and denominator. The numerator and denominator can be of length 1-50 and must be greater than 0. The numbers are generated randomly resulting in the need of storing decimal places. In my code I convert the string to BigInteger first and<\/li>\n<\/ul>\n<p>Web site is in building<\/p>\n<p>I discovery a place to host code\u3001demo\u3001 blog and websites.<br \/>\nSite access is fast but not money<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.m5zn.com\/newuploads\/2014\/01\/30\/jpg\/e7da807964b1fff.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>the Tin Man rubygems bundle bigdecimal gemfile I&#8217;m using Bundle to install gems in d:\/cucumbertests\/cucumber\/ successfully, which installs BigDecimal v 1.1.0. This failed in d:\/cucumbertests\/cucumber\/itinfix which installed BigDecimal v 1.2.0.How do I identify the correct version of BigDecimal in my Gemfile in d:\/cucumbertests\/cucumber\/itinfix? Why is the version of BigDecimal correct in d:\/cucumbertests\/cucumber? The latest version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7278","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7278"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7278\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}