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
2 answers

BigDecimal.Divide gives incorrect result

I am getting incorrect result while performing the division using BigDecimal. number1 = 221500.0 number2 = 12.0 Here is my code snippet: BigDecimal d1 = new BigDecimal(String.valueOf(number1)).setScale(13, 4); BigDecimal d2 = new…
Suman
  • 21
  • 1
  • 5
0
votes
1 answer

Multiplying bigdecimal number selected from database

I select a number column in database. For example: BigDecimal total_price_bg; BigDecimal unit_price_bg; if (results.next()) { unit_price_bg = results.getBigDecimal("UNIT_PRICE"); …
sg552
  • 1,521
  • 6
  • 32
  • 59
0
votes
1 answer

Why does a BigDecimal scale changes if accessed through an association?

I have two Ruby on Rails models Farm and Harvest. A farm belongs to a harvest. Here are the models: class Farm < ActiveRecord::Base acts_as_singleton belongs_to :harvest validates :harvest, presence: true, allow_blank: true serialize…
Sofia Bravo
  • 579
  • 8
  • 22
0
votes
0 answers

double to BigDecimal results in a 4 instead of 5 in the last place

I have a double value which is shown as: (created from a string exactly like this) 2.4284723894328895E13 If I run this (new BigDecimal(doubleValue)).toPlainString() I get (java.lang.String) 24284723894328.89453125 I am confused. Why are the…
Antje Janosch
  • 1,154
  • 5
  • 19
  • 37
0
votes
1 answer

java.math.BigDecimal in p:selectOneMenu

Converter : @FacesConverter("bigDecimalConverter") public class BigDecimalConverter implements Converter { private static final int SCALE = 2; @Override public Object getAsObject(FacesContext context, UIComponent component, String…
Tiny
  • 27,221
  • 105
  • 339
  • 599
0
votes
0 answers

BigDecimal to Fraction - Errors in repeating numbers

I've been attempting to convert a decimal number to a rational, where the decimal is an instance of BigDecimal. This works perfectly find for all terminating decimals, however non-terminating decimals cause rounding errors, and I don't know how to…
Socratic Phoenix
  • 556
  • 1
  • 7
  • 19
0
votes
2 answers

NoMemoryError: failed to allocate memory using BigDecimal .to_s method

I use Ruby 2.2.3 and Rails 4.2.3. I get an NoMemoryError: failed to allocate memory using the following code in irb console: # Using 123e+1000000000000000000 BigDecimal('123e+1000000000000000000').to_s #=> NoMemoryError: failed to allocate…
phlegx
  • 2,618
  • 3
  • 35
  • 39
0
votes
1 answer

BigDecimal - ArithmeticException even with exact representable result

BigDecimal bd1 = new BigDecimal(8757).divide(new BigDecimal(12)); I get the following exception: Caused by: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. Why is it the case? The result of…
manash
  • 6,985
  • 12
  • 65
  • 125
0
votes
1 answer

BigDecimal comparison issue in method

I am trying to filter products as per price range of high and low. And this is the URL i'm testing with http://localhost:8080/webstore/products/test?high=900&low=100 This is my Repository(ProductRepository) public interface ProductRepository { …
user3127109
  • 3,421
  • 8
  • 24
  • 33
0
votes
2 answers

BigDecimal Dividing with uneven decimals

I am trying to work on a program for work with multiple BigDecimal calculations necessary. I am at quite a confusing point because the BigDecimal is not cooperating how I would like. Let me explain what I need it to do: First it takes a monetary…
jesric1029
  • 698
  • 3
  • 10
  • 33
0
votes
1 answer

Both BigDecimal and DecimalFormat give wrong output

I've done a lot of research on this, but I just can't seem to get the right answer. I need the final result to be in a specific format, but no matter what I do, my code doesn't work for larger numbers. It works for small numbers, but for larger ones…
imaginedrragon
  • 303
  • 3
  • 18
0
votes
2 answers

How to implement parameterized arithmetic

I am designing a library that provides a huge collection of classes that perform heavy operations with numbers. I wouldn't like to limit users of the library to a single number type such as int, double, or BigDecimal - and so it would be nice if I…
Parker Hoyes
  • 2,118
  • 1
  • 23
  • 38
0
votes
1 answer

How to calculate the ratios of values to their sum?

I have 3 BigDecimal values and I want to find 3 values that represent its ratio to their sum and those ratios should add up to 1. However, I'm running into the following error: BigDecimal a = new BigDecimal("19000.0000"); BigDecimal b = new…
Glide
  • 20,235
  • 26
  • 86
  • 135
0
votes
1 answer

Formatting Bigdecimal. JavaFX

I want to print BigDecimal in TableColumn in my JavaFx application. But I can't properly format it. I've tried this: DecimalFormat df = new DecimalFormat("#,###.00"); tc_ma_sell_amount.setCellValueFactory(cellData -> new…
rakamakafo
  • 1,144
  • 5
  • 21
  • 44
0
votes
0 answers

Convert stream into Big-decimals

I want to convert stream into BigDecimal object. My code: @Named @Dependent public class NoteSumOrderValidator implements RosterIntegrityConstraintValidator { @Inject private MessageBundle bundle; @Override public…
Jack
  • 209
  • 1
  • 3
  • 10