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

I want to evalutate the cents field.

Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76

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

Joined: Sep 18, 2000
Posts: 582
    
    7

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

Joined: Mar 05, 2007
Posts: 76
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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Mar 05, 2007
Posts: 76
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
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Mar 05, 2007
Posts: 76
I am getting a integer. a Number field....from database.
Then I use a While stmt like below.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You said integer, but your code says getDouble().
Justin Char
Ranch Hand

Joined: Mar 05, 2007
Posts: 76
I thought a double, int and integer are all the same
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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

Joined: Oct 02, 2003
Posts: 11498
    
  16

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

Joined: Mar 05, 2007
Posts: 76
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I want to evalutate the cents field.