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

Power doesn't seem to work with BigDecimal (Java)

I have made a little code. So far I just built a do while loop that works with BigDecimal. To be more precise, the counter variable of the loop is a BigDecimal. I already put much effort to it although it might not look like that (it's short).. But…
cnmesr
  • 345
  • 3
  • 11
0
votes
1 answer

how to properly deal with bigdecimal values in ormlite?

Is it possible to use sum() function for bigdecimal column in sqlite, since they are stored as varchar? I'm using ormlite for that.
0
votes
1 answer

How can I write a Junit test using assertEquals to see how many of each coins will be returned to me using?

I need to write a test using assertEquals to see how many of each coins will be returned to me when money is given and am not sure how to structure it or what info to include. Money can be inserted in the amounts of $1, $2, $5 and $10. Change…
0
votes
2 answers

How to convert dollar amount to cents in java?

I have the following program (taken from SO link What is the best way to convert Dollars (Big Decimal) in Cents (Integer) in java?) which converts dollar to cents. However, the output is not as per my expectations. Current Output: 12345 8 Expected…
Nital
  • 5,784
  • 26
  • 103
  • 195
0
votes
2 answers

Why does BigDecimal divide cause rounding when it should fit within the significant digits?

Why does BigDecimal divide cause rounding to happen even though the value should fit comfortably in 4 significant digits? BigDecimal realPercent = new BigDecimal(1.25, new MathContext(4)); // 1.25 BigDecimal percentDivider = new BigDecimal(100, new…
Kit Sunde
  • 35,972
  • 25
  • 125
  • 179
0
votes
1 answer

App script spreadsheets float calculation, can google app script do float point number calc?

Can google app script use external library in google app script that can do float number calculation, like bigdecimal? when I do var i = 1.15 - 1.12 console.log(i); then i = 0.029999999999999805
0
votes
2 answers

java double to BigDecimal convertion adding extra trailing zero

double d =0.0000001; BigDecimal d1 = BigDecimal.valueOf(d); System.out.println(d1.toPlainString()); prints output => 0.00000010 i am expecting 0.0000001
user7951871
  • 3
  • 1
  • 2
0
votes
1 answer

how to negative bigdecimal's value in android

i have a Big Decimal like this for example. i want to negative it's value. it has a string. String a = "65"; BigDecimal example = new BigDecimal(a); //i want to have (-65)
sorena
  • 3
  • 2
0
votes
1 answer

JavaL Different Big Decimal values converted to UTF-8 Strings have the same value

As part of having fun with Avro, I discovered the following: new String(new BigDecimal("1.28").unscaledValue().toByteArray(), Charset.forName("UTF-8")) .equals( new String(new BigDecimal("1.29").unscaledValue().toByteArray(),…
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87
0
votes
1 answer

How to write code more elegantly? (Factorials, BigDecimals, division by BigIntegers)

I've managed to make my code working, but feel like there's a better approach to writing similar stuff, any tips or mistakes to point out? Here's my code: public static void main(String[] args) { DecimalFormat df = new…
0
votes
2 answers

Array in scala produced by x to y by z resulting in long decimals

I am trying to generate a Numeric Range of Double by val arrayOfDoubles = (0.0 to 1.0 by 0.1).toArray, but the resultant is not what I expected it to be. the result is something like Array(0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6, 0.7,…
atalpha
  • 320
  • 1
  • 7
  • 19
0
votes
1 answer

How to convert any type of Number into BigDecimal without toString()?

I'm using now this method, to execute addition on any type of Number: public CustomNumber add(T a, T b) { BigDecimal numberA = new BigDecimal(b.toString()); BigDecimal numberB = new BigDecimal(a.toString()); return new…
dmbdnr
  • 334
  • 3
  • 19
0
votes
2 answers

Big Decimal parsing problem

I am facing problem in big decimal no. Following code snippet will explain my problem: BigDecimal parsedValue = (BigDecimal) decimalFormat.parse(input); Here input is a string type. Now suppose value of input is 135abc24 in this case value of…
Harry
  • 75
  • 2
  • 10
0
votes
1 answer

Multiplying a BigDecimal with Integer in android studio

I am writing a formula in an app I am creating. This includes multiplying. I have managed to multiply BigDecimals but cannot figure out how to do this with an integer. Here is my java code. public void onClick(View v) { //Get string…
0
votes
1 answer

Not removing leading zero's in Big Decimal in java

I want to write 5 digit zip code to an Excel file. for ex. for number 01803 if I don't convert it to string it will store it as 01803 string but shows an error on excel file.So I want to store it as Big Decimal number without stripping the leading…
vk1
  • 166
  • 6
  • 15