I was solving a java HackerRank problem in which I had to sort an array consisting decimal numbers using BigDecimal class in descending order. The solution works fine except for one case in which 0 and 000.000 comes. Now as they are equal, the problem tells us not keep them in the same order of their occurrence, but it is

**Advertisement**

# Tag: bigdecimal

## How to make BigDecimal division more precise

I’m having an issue with BigDecimals, the simplified idea is to: define a value for the total split the total in 3 parts defined by weights, these weights are 3 double values that add up to 100.0 sum up the parts the sum should be close to the total, the error should be at most 0.00000001 Here’s the failing test:

## why is my primality test failing so often when randomizing a BigInteger?

I wasn’t able to get true for both p and q, most of the results is both false or rarely p is true but q is false, why wouldn’t this test ever be true for both p and q? BigInteger bitSize100 = new …

## Create BigDecimal from unscaled long

I’m trying to convert long 1099 to BigDecimal 10.99; This gives me 11.00: AFAIK this should work. What’s my bonehead error? Answer The error is that there’s a distinction between scale and precision. The constructor of MathContext accepts a precision, which is a total number of decimal digits on either side of the decimal point. (For example, the original BigDecimal

## BigDecimal gives unexpected results for numbers larger than 7 or 0 decimal numbers

While trying to calculate a ratio of the volume of 2 objects, I noticed some weirdness in the calculation, here is a sample you can run for yourself: public class TestApplication { public static …

## How likely is BigDecimal to cause memory issues for a POS program?

I understand that BigDecimal is the most accurate way to express currency because treating currency values as a floating-point data type tends to cause rounding errors. However, I also understand that …

## How to get the BigDemical in SQLite?

I’m grabbing the data of the database. If integer I can use getInt, If String I can use getString. But BigDecimal I don’t know what to use… Please Help. Thanks in advance! Answer Use BigDecimal.valueOf(double val):

## How to multiply a BigDecimal by an integer in Java

How do you multiply a BigDecimal by an integer in Java? I tried this but its not correct. Answer 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:

## BigDecimal – to use new or valueOf

I came across two ways of getting BigDecimal object out of a double d. new BigDecimal(d) BigDecimal.valueOf(d) Which would be a better approach? Would valueOf create a new object? In general (not just BigDecimal), what is recommended – new or valueOf? Answer Those are two separate questions: “What should I use for BigDecimal?” and “What do I do in general?”

## How to change the decimal separator of DecimalFormat from comma to dot/point?

I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df….

**Advertisement**