• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increase the range of Double

 
Steve Howerton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5888
63
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or BigDecimal.
 
Steve Howerton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except maybe Zimbabwean dollars; the difference between 100 and 1000 was so small you wouldn't even notice
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I forgot. Zimbabwean dollars don’t have absolute values. They shrink as you watch. Sorry for the mistake.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5888
63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They started writing the denominations in scientific notation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has shrunk so much it has vanished up its own *****, you mean.
 
Steve Howerton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic