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

How to force number field to be parsed as Double even if it is Int using play.api.libs.json library in Scala

Bellow is the snippet from Scala REPL, how do I make a json like {"a":2.0}? scala> val f = 2.0 f: Double = 2.0 scala> f res2: Double = 2.0 scala> val x = play.api.libs.json.Json.obj("a" -> f) x: play.api.libs.json.JsObject = {"a":2}
lprakashv
  • 1,121
  • 10
  • 19
0
votes
2 answers

BigDecimal scale for large numbers

I'm trying to understand the scale for a BigDecimal but it acts weird and I can't understand why. Here's a couple of examples: Double d = new Double(1000000d); int scale = new BigDecimal(d.toString()).scale(); The scale in this example will be 1…
0
votes
1 answer

How to do mod (10^9+7) for large numbers in Java

Coding puzzles generally ask to give result mod (10^9+7). Refer the explanations here. Suppose I have a 2 very big number; say 2 large string of numeric characters. I need to add them and result processed as mod (10^9+7). How can i achieve that.…
Kaushik Lele
  • 6,439
  • 13
  • 50
  • 76
0
votes
0 answers

How to sum BigDecimal as class variables

I have this class: public class Period { private BigDecimal periodAmount; public BigDecimal getPeriodAmount() { return periodAmount; } public void setPeriodAmount(BigDecimal periodAmount) { this.periodAmount = periodAmount; } And I was…
Felipe Wagner
  • 154
  • 2
  • 14
0
votes
3 answers

BigDecimal incompatible with ArrayList, java ClassCastException

I'm debugging some code from a different project in Eclipse, and found that even though there is no error sign (just warning), try { conn = dss.connect(); System.out.println("[SIGN]: Executing first step "); …
Mokz
  • 124
  • 1
  • 16
0
votes
1 answer

Byte to value error

So in c#, I have needed a random below given number generator and I found one on StackOverFlow. But near the end, it converts the byte array into a BigInteger. I tried doing the same, though I am using the Deveel-Math lib as it allows me to us…
no name
  • 72
  • 6
0
votes
2 answers

Pad zeros if precision value less than scale

If i divide 1.0f/2.0f , the value is 0.5 . I wanted to have a precision value of 5 (0.50000). Used BigDecimal to set the scale to 5 but still the value is printed as 0.5 BigDecimal val = new BigDecimal(1.0f/2.0f); BigDecimal tt = …
0
votes
1 answer

Square Root and ++ Operators for BigInteger and BigDecimal

Is there any ready-made Java library for operations over BigInteger and BigDecimal objects? I'd like to use square root and ++ operators. Thank you P.S. BigInteger.add() should be used instead of ++, I got it. What about square root from BigInteger?…
sunny
  • 1,887
  • 3
  • 29
  • 40
0
votes
2 answers

print number with 2 digits but round with 6 digits of precision

For getting a String with exactly 2 digits after the "." I can do something like: DecimalFormat df = (DecimalFormat)DecimalFormat.getInstance(Locale.US); df.applyPattern("0.00"); df.setRoundingMode(RoundingMode.HALF_UP); …
Jens
  • 495
  • 1
  • 6
  • 28
0
votes
1 answer

Displaying BigDecimal as Integer while Keeping Original Value

The purpose of this program is to give correct change. For Example: Input: $45.54 Output: 4 Ten Dollar Bills, 1 Five Dollar Bills, 2 Quarters, 4 Pennies. Now onto my question: I want to display a BigDecimal as an integer without losing the original…
Zach Davis
  • 27
  • 1
  • 5
0
votes
2 answers

How to check if a Double has java rounding?

Is there anyway to check if a Double has java rounding e.g. -260.01079999999996 I have one system that passes a computed Double value. By right should be -260.0108.
bittersour
  • 937
  • 2
  • 11
  • 32
0
votes
1 answer

Keep up to 3 decimals without doing any rounding

I have a double that I want to keep only 3 decimal places but without applying any rounding at all. E.g. 92.36699 should be 92.366 I tried the following: DecimalFormat nf= new DecimalFormat("#0.000"); String number = nf.format(originalNumber); …
Jim
  • 18,826
  • 34
  • 135
  • 254
0
votes
3 answers

Double to BigDecimal Java

What would be the best way of changing all of these double values to BigDecimal? I guess I did not realize when I started I should use BigDecimal for financial based programs. I am quite new to Java and appreciate any and all feedback. private…
Gabriel_W
  • 1,645
  • 5
  • 16
  • 26
0
votes
1 answer

How to produce the BigDecimal NumberFormatException and resolve it?

I am using the code snip from spray json deserialize in my scala code. https://github.com/spray/spray-json import spray.json._ import DefaultJsonProtocol._ object MyJsonProtocol extends DefaultJsonProtocol { implicit object…
sk1007
  • 571
  • 3
  • 11
  • 30
0
votes
1 answer

Java BigDecimal's negative fraction to binary form

I need advice how to properly convert negative fractional part to binary. My custom data type with fixed point need be converted to byte[]. So, currently I need to implement such conversion: BigDecimal -> byte[] -> BigDecimal As I understand,…
Constantine
  • 1,802
  • 3
  • 23
  • 37