Solved

# math with BigDecimal, stuck

Posted on 2007-10-17
1,670 Views
I want to do: <%= 100.0 * rs.getBigDecimal("DROPirate")) %> Of course, that doesn't work because it gives the error: "The operator * is undefined for the argument type(s) double, BigDecimal". I've tried various things, but my beginner status isn't helping me make much progress. How to I do this?
0
Question by:jmarkfoley

LVL 86

Accepted Solution

Try

<%= new BigDecimal("100").multiply(rs.getBigDecimal("DROPirate")) %>
0

LVL 26

Expert Comment

and try this,

<%= 100.0 * rs.getBigDecimal("DROPirate").doubleValue() %>
0

LVL 86

Expert Comment

>><%= 100.0 * rs.getBigDecimal("DROPirate").doubleValue() %>

Problem with that is that it constrains precision, which defeats the object of using BigDecimal
0

LVL 92

Expert Comment

why get it as a BigDecimal in the first place, would appear to be just complicating things unless u have a specific need:

<%= 100.0 * rs.getDouble("DROPirate") %>

you can use a DecimalFormat (or format tag) to then format it as required.
0

LVL 86

Expert Comment

jmarkfoley,  i of course am assuming you are not getting BigDecimal from the ResultSet for no good reason
0

LVL 1

Author Comment

CEHJ: Yes, decimal for a reason. The data is stored in the SQL server database as decimal and converting it to double does introduce rounding errors. I have to accumulate a running total to output and the rounding errors on floating point can add up to more than a few cents. So, I'd rather do the painful manipulation of BigDecimal. Your solution worked. I had to include:
<%@ page import="java.math.BigDecimal" %>
but, I have a concern. If I do 'new BigDecimal(...' for each row I retrieve, am I not creating lots of new BigDecimal objects?
0

LVL 11

Assisted Solution

I wouldn't worry about object creation, it's not really an issue.  If you like, you can create a 100 BigDecimal and reuse it (this is safe, since the BigDecimal is immutable).
0

LVL 86

Expert Comment

:-)
0

LVL 92

Expert Comment

> The data is stored in the SQL server database as decimal and converting it to double does introduce rounding errors.

you're going to get rounding issues whatever you use as you can only display it to a fixed number of places :)
0

LVL 1

Author Comment

objects: thanks for your feeback. You've been a big help on lots of other questions  I've posted. I appreciate you insights.

Regarding rounding on decimals. The problem in converting decimal to double is that often things like 23.45 get converted to 23.4499999 or 23.450001 which is no problem once individually formatted. The problem comes in when those values are aggregated in a sum. Depending on how many values are added, they can accumulate several hundredths of rounding error. If a value is stored in database as decimal with only two digits of precision, then you will never get rounding errors to a even a third digit, let alone in the second digit. I've had experience both ways, esp. with languages like C or shell CGI scripts that don't have a decimal datatype. This is one reason why COBOL hung around so long - native decimal datatype.
0

LVL 92

Expert Comment

in this case your not aggregating them, you're just displaying them so creating a BigDecimal is unnecessary overhead.
0

LVL 86

Expert Comment

>>you can only display it to a fixed number of places :)

That's not the case with, and is really the whole point of, BigDecimal.
0

## Featured Post

### Suggested Solutions

Convert ArrayList of custom objects to JSON 4 44
copyEndy  challenge 15 38
sumHeights2  challenge 7 51
Fibonacci challenge 11 61
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.