Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I want to evalutate the cents field.

 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have the amount field formatted like above.

I want to say If last two digits which are the cents digits are greater than zero. (do something else)

does anyone have an idea how I would accomplish this?

Thank You in advance Justin
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sort of like saying if the remainder of the number after dividing by whole dollars is greater than zero than ........

look at the % (mod or modulus) operator.

steve
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand how mod will work.

I tried to use (substring,1,6) (substring,7,2)

But being it is a double I can't use substring.

I don't understand how I can use my SQL to grab the cents.

With out checking the length like


can you get me closer. Its not a string so above won't work. But that is the way I am thinking I will have to go.

Thank you in advance

Justin
 
fred rosenberger
lowercase baba
Bartender
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to figure out how to pull apart the number to get dollars and cents, or are you trying to print it out differently when the cents part equals 0?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ought not to use double for money in the first place. In MySQL you would use the DECIMAL datatype; you will have to check the types returned, but I think that would come out as a BigDecimal. There are remainder and IEEERemainder methods in BigDecimal; you want the ordinary one.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry if I am not asking the right questions.

Here I will try to better.

What the problem is I am accessing a Sql database and grabbing data
Then running it through some if and else stmts.

When I get a transaction that has a 1.00 or 100.00 I want to write out a different type of transaction

When the amt is 1.30 or 100.20 I want to write out a different trx.

I am using a number formatter that gives me a 8 digit double field that place zeros in front of the amount so the field is a total of 8 characters with no commas or periods which it must be to write out my transactions.

I just want to be able to say anything greater than 00000000 in positions 7 and 8 please write this transaction. If positions 7 and 8 equal to zero write a different transaction out. I hope you understand what I am trying to do. If not please let me know and I will try to explain better.
Thank you for trying to help me.
Justin
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Positions 7 and 8? You still haven't said what type of data you are getting from the database. Is it a string or a number? If so what kind of number is it? I'm talking about the type of the Java variable to which you are assigning this value.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting a integer. a Number field....from database.
Then I use a While stmt like below.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said integer, but your code says getDouble().
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought a double, int and integer are all the same
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You said integer, but your code says getDouble().

Or it says getString(). The getDouble() calls aren't assigned to any variable so there isn't any question of formatting those double values. I still think we need a coherent question with an example of some code that isn't doing something right, and explanations of what it's doing and what it would be nice for it to do instead.

Right now there's no question on the table here, just hand-waving.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12181
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin Charpenter:
I thought a double, int and integer are all the same


A double is for a floating point number - something like 8.2 or 987.33442 or .00003445. An int (which is an abbreviation of integer) is for numbers without a decimal point: 57, -393848 or 0

Internally, they are stored differently and have different possible ranges. Floating point numbers may not be 100% accurate due to rounding issues and the precision to which they can be stored.

So to be brief, a double and an int are NOT the same thing.
 
Justin Char
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Steve and everyone that helped.


This is what I wanted to do. This works and does exactly what I wanted.
When Steve said Modulus I looked at it. Didn't make sense. Then a friend mentioned Modulus again so I looked at it a second time and came up with this. My fried said it is just simple math and I didn't look at it that way.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That output suggests you are storing an int in a double field. You can get away with it if the values are small, but if you are storing ints your field type should be int.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic