Questions tagged [bigdecimal]

BigDecimal is a numeric object type in Java that represents decimal numbers with arbitrary precision.

Because double and float use a fixed amount of memory (64 and 32 bits respectively), they have limited precision which can lead to rounding errors, and more importantly, they cannot be used to represent all decimal fractions exactly as they use two's complement.

BigDecimal solves these problems by providing arbitrary precision and by using a decimal representation of their values.

The BigDecimal class also provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

More information can be found in the BigDecimal Javadoc

1677 questions
0
votes
1 answer

ClassCastException: java.lang.Double incompatible with java.math.BigDecimal

This exception is coming in very strange way. I am reading a variable's value from a stored procedure whose type is DECIMAL(15,4). And at java end I was type casting this value to BigDecimal, and it was working perfectly fine. Now I started getting…
Onki
  • 1,879
  • 6
  • 38
  • 58
0
votes
1 answer

Issue with scale and rounding of BigDecimal

Basically I wrote a method for reciprocal value of a BigDecimal instance: public class Main{ public static void main(String[] args) { BigDecimal value1 = new BigDecimal("88"); BigDecimal reciproc1 = reciproc(value1); BigDecimal…
void
  • 731
  • 2
  • 11
  • 26
0
votes
1 answer

Java Map String BigDecimal get function

public Set getProductsBypriceFilter(Map filterParams) { Set productsByPrice = new HashSet<>(); Set criterias = filterParams.keySet(); if (criterias.contains("low")) { for (Product…
user3551808
  • 95
  • 2
  • 8
0
votes
1 answer

DecimalFormat pattern for BigDecimal.

What pattern of DecimalFormat should I use so that when I format BigDecimals like new BigDecimal("4235886589.00000"); new BigDecimal("4235886589.0000030000"); new BigDecimal("4235886589.0"); new BigDecimal("4235886589"); The output must have the…
void
  • 731
  • 2
  • 11
  • 26
0
votes
1 answer

Calculate Euro Prices Using BigDecimal

I have a string with the price of 70,00 and another string with the price of 25,00 I want to add them (find the sum), and I have concluded r=that I have to use BigDecimal to accomplish it. Here's my code: String CPUprice = "70,00" …
0
votes
1 answer

can we remove floating zero before decimal point in BigDecimal

BigDecimal bd1= new BigDecimal(0.000); bd1 = bd1.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros(); System.out.println("bd1 value::"+ bd1); I get the following 0.00 for bd1, but I want bd1 as .00 not as 0.00. Am I applying the methods…
Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
0
votes
2 answers

How can BigDecimal make no floating point inaccuracy?

I have known there is floating point inaccuracy regardless of OS, programming language. I, however, found there is no inaccuracy in this example. How can be this possible? I think converting stirng to double is needed for calculation, so there…
Volnyar
  • 11
  • 4
0
votes
1 answer

Do I get wrong output with BigDecimal ROUND_HALF_EVEN rounding?

I have a problem with rounding of BigDecimal numbers using ROUND_HALF_EVEN as described here. I want to round to 2 decimal places: BigDecimal number1 = new BigDecimal("23.867995"); BigDecimal number2 = new BigDecimal("23.868"); Log.d("tag",…
Pa3k
  • 31
  • 2
0
votes
3 answers

Remove the leading 0's till the decimal

I want to remove the leading zeros in the decimal numbers. So i want the output should be .324 not 0.324. I tried str.replaceFirst("^0+(?!$)", ""); Didn't work and i also tried the regex! No results! And yes I am working with BigDecimal.
Vishal
  • 391
  • 5
  • 20
0
votes
1 answer

Convert double (0.1) to BigRational and back

I must be missing something, but when I try to create a BigRational from double value 0.1 it gives me a really long nonsense value. Converting it back gives 0.0: double d = 0.1; // 0.1 BigRational br = new BigRational(d); //…
kcnygaard
  • 794
  • 7
  • 18
0
votes
1 answer

Why are the calculated values different for the binomial upper cumulative probability?

I am trying to calculate the upper cumulative probability of the binomial distribution in java. Since the probability values can go outside the double range, I am using BigDecimal. An example for total number of trials is n = 403 and number of…
Kaleido
  • 3
  • 2
0
votes
1 answer

Ruby BigDecimal : increase number of initial digits saved in database

I am facing an issue with Ruby BigDecimal on my Rails 4 app. I have a "price" attribute in a Thing model, which class is BigDecimal. Whenever I create or update a Thing, I would like to save the "price" attribute with strictly two decimal digits…
Sag
  • 35
  • 4
0
votes
1 answer

Error when exporting jasper reports to pdf

I am getting the error below when I try to export jasper report to pdf. Any help will be highly appreciated. run: Compiling Report Design ... net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class…
0
votes
2 answers

Performance Improvement For Using BigInteger While Calculating Square Root

I am trying to calculate the square root of all the integers below 100 with A precision of up to 10000 digits. I already tried it using Newton's method with Big Decimal, where it eats a lot of time. So now am using Jarvis method for finding the…
ArunKumar
  • 19
  • 1
  • 1
  • 6
0
votes
4 answers

Java : Rounding a decimal value to HALF_EVEN

I have been trying to write a java code to Round a value to the below requirement. If x=63.88 => roundedValue= 64.00; If x=63.50 => roundedValue= 64.00 If x=63.32 => roundedValue= 63.32 I tried with the different roundingModes like CEILING, DOWN,…
shockwave
  • 3,074
  • 9
  • 35
  • 60