File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Increase the range of Double Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Increase the range of Double" Watch "Increase the range of Double" New topic
Author

Increase the range of Double

Steve Howerton
Ranch Hand

Joined: Oct 18, 2011
Posts: 41
Hello all.
In my Apache derby db I set up Decimal data types like this:


I use JFormattedTextFields to filter decimal usage in text fields concerning money
Like admin pay rate would be 150.00: this is good.

In the JFormattedTextField it will allow 1,000.00 but the db crashes when this
Decimal is presented.

Will I have to change the db data type ?


I am not sure about maxValue in my class. Because I am not sure what is a good value
Or how to change maxValue to restrict a value that exceeds what the data base considers
A double.

Would someone know the best way to coordinate the field with the db ?

It works ok for now but the user has the ability to present number like
1,000,000,000.00 and in most of the values I am working with are not
that big.



error



Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Do NOT use floating point values to store money. Never. Ever. Hit yourself with a stick when you try doing it.

Floating point values are an approximation. You wouldn't like your bank to remember the approximate amount in your bank account, would you?

Instead, use exact values, like integers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
. . . or BigDecimal.
Steve Howerton
Ranch Hand

Joined: Oct 18, 2011
Posts: 41
Thanks I have some studying to do. I will have to try to use the BigDecimal data type in apache derby db. All my forms collect values for double. The JFormattedTextFields wlll allow input of doubles of any size so maybe I will just have to re-write and convert them to BigDecimal in order to save them. When I address this
I will try to supply some code to show a simple example for using BigDecimal.
I have not found to many examples stressing the use of BigDecimal thou it
is an essential concept.
Thank you for your time.

Compound interest
http://www.coderanch.com/forums/posts/preList/552780/2507967
BigDecimal . RoundingMode and MathContext


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Steve Howerton wrote: . . . Compound interest
http://www.coderanch.com/forums/posts/preList/552780/2507967
BigDecimal . RoundingMode and MathContext
Who wrote that code? I wouldn’t believe anything he says!
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Stephan van Hulst wrote:Do NOT use floating point values to store money. Never. Ever. Hit yourself with a stick when you try doing it.

When I teach Java at the local university, I spend a half lecture on this topic.

Listen to @stephan. Never use floating point for money. Not for dollars, euros, swiss francs, yen, etc. Never. Don't do it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Except maybe Zimbabwean dollars; the difference between 100 and 1000 was so small you wouldn't even notice


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8186
    
  23

Steve Howerton wrote:In my Apache derby db I set up Decimal data types like this:I use JFormattedTextFields to filter decimal usage in text fields concerning money
Like admin pay rate would be 150.00: this is good.

In the JFormattedTextField it will allow 1,000.00 but the db crashes when this
Decimal is presented.

Will I have to change the db data type ?

Going back to your original question, the answer is YES. If you need to pass a value that is more than 999.99, then you will have to change the database definition, regardless of what Java type you use.

However, you should still heed the good advice you've been given.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

Rob Spoor wrote:Except maybe Zimbabwean dollars; the difference between 100 and 1000 was so small you wouldn't even notice

Hahah
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Not at all. It is still larger than the difference betweend1 and d2. Ten times. Just because the absolute values are small, that doesn’t vitiate the difference.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
Oh, I forgot. Zimbabwean dollars don’t have absolute values. They shrink as you watch. Sorry for the mistake.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8186
    
  23

Campbell Ritchie wrote:Oh, I forgot. Zimbabwean dollars don’t have absolute values. They shrink as you watch. Sorry for the mistake.

Actually, I believe it is no more. It has ceased to be. This, from Wikipedia:

"..the use of the dollar as an official currency was effectively abandoned on 12 April 2009. This was a result of the Reserve Bank of Zimbabwe legalizing the use of foreign currencies for transactions in January 2009.
Currencies such as the South African rand, Botswana pula, pound sterling, euro, and the United States dollar are now used for all transactions in Zimbabwe; the policy of the government of Zimbabwe has insisted that any attempts to reintroduce Zimbabwean currency should be considered only if industrial output improves.[5]..."

Not surprising I suppose when they had a Z$100-trillion note. I imagine the ink to print the zeroes was a a limiting factor.

Winston
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

They started writing the denominations in scientific notation.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
It has shrunk so much it has vanished up its own *****, you mean.
Steve Howerton
Ranch Hand

Joined: Oct 18, 2011
Posts: 41
Thanks for the input. I really can not offer a good code example but I can say that I use Netbeans and I had to insure that BigDecimal was imported because it was
Allowing me to use methods from the pkg but there was no import for BigDecimal and it did not indicate it. I found very few examples but the Compound Interest example
Mentioned does offer some relative conventions.
Thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
The problem is that a lot of people who teach computing don’t know about it, so you get few examples in tutorials. People who write banking applications know all about BigDecimal, and they always post their code on the net, don’t they.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Increase the range of Double