What is the equivalent of the Java BigDecimal class in C#?

Just recently I also needed an arbitrary precision decimal in C# and came across the idea posted here: https://stackoverflow.com/a/4524254/804614 I then completed the draft to support all basic arithmetic and comparison operators, as well as conversions to and from all typical numerical types and a few exponential methods, which I needed at that time. It … Read more

Why BigDecimal(“5.50”) not equals to BigDecimal(“5.5”) and how to work around this issue?

From the javadoc of BigDecimal equals public boolean equals(Object x) Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). Simply use compareTo() == 0

Use of java.math.MathContext

For rounding just the fractional part of a BigDecimal, check out the BigDecimal.setScale(int newScale, int roundingMode) method. E.g. to change a number with three digits after the decimal point to one with two digits, and rounding up: BigDecimal original = new BigDecimal(“1.235”); BigDecimal scaled = original.setScale(2, BigDecimal.ROUND_HALF_UP); The result of this is a BigDecimal with … Read more

Square root of BigDecimal in Java

I’ve used this and it works quite well. Here’s an example of how the algorithm works at a high level. Edit: I was curious to see just how accurate this was as defined below. Here is the sqrt(2) from an official source: (first 200 digits) 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147 and here it is using the approach I outline … Read more

Check if BigDecimal is an integer in Java

EDIT: As of Java 8, stripTrailingZeroes() now accounts for zero BigDecimal stripTrailingZeros doesn’t work for zero So private boolean isIntegerValue(BigDecimal bd) { return bd.stripTrailingZeros().scale() <= 0; } Is perfectly fine now. If you use the scale() and stripTrailingZeros() solution mentioned in some of the answers you should pay attention to zero. Zero always is an … Read more

How to multiply a BigDecimal by an integer in Java

You have a lot of type-mismatches in your code such as trying to put an int value where BigDecimal is required. The corrected version of your code: public class Payment { BigDecimal itemCost = BigDecimal.ZERO; BigDecimal totalCost = BigDecimal.ZERO; public BigDecimal calculateCost(int itemQuantity, BigDecimal itemPrice) { itemCost = itemPrice.multiply(BigDecimal.valueOf(itemQuantity)); totalCost = totalCost.add(itemCost); return totalCost; } … Read more

JUnit Assert with BigDecimal

The official junit solution to assert that two BigDecimal are matematically equal is to use hamcrest. With java-hamcrest 2.0.0.0 we can use this syntax: // import static org.hamcrest.MatcherAssert.assertThat; // import org.hamcrest.Matchers; BigDecimal a = new BigDecimal(“100”) BigDecimal b = new BigDecimal(“100.00”) assertThat(a, Matchers.comparesEqualTo(b)); Hamcrest 1.3 Quick Reference

Rounding Bigdecimal values with 2 Decimal Places

I think that the RoundingMode you are looking for is ROUND_HALF_EVEN. From the javadoc: Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for ROUND_HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for ROUND_HALF_DOWN … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)