This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes comparing the range of BigDecimal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "comparing the range of BigDecimal" Watch "comparing the range of BigDecimal" New topic
Author

comparing the range of BigDecimal

Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hi,

I have a requirement, where I need to check the BigDecimal value in a particular range.

Eg: val>=15,001.01 and val<=25,001.01 set newval=5.000

Could you any one suggest me how to do this?

I tried using the following approach but the result is not as expected.

if(var.doubleValue()>=15001.01&&var.doubleValue()<25001.01)

newvar=new BigDecimal(5.00);

I am not sure if compareTo can help me use the above logic, because I have many more ranges to be compared(15,001.01-25,001.01;25,001.01-50,001.01 etc...).

What would be the best way to resolve this issue.

Thank you.


SCEA part I,TOGAF Foundation
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
BigDecimal implements the Comparable interface.

I would create a method like this


[ March 13, 2006: Message edited by: Keith Lynn ]
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Thanks for replying Keith.

However I have to use the following comparision. How can this be achieved using compareTo?

In Main program I have the following line...

var=getNum(val)

y.setVal(x);

public BigDecimal getNum(BigDecimal val){

if (var>=15001.01 and var <25,001.01)

x=new BigDecimal(5.00);

if (var>=25,001.01 and <50,001.01)

x=new BigDecimal(10.00);

return x;

}

I have 7 more such comparision in the getNum method..... any further ideas how this can be implemented.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367


You could do something like this


[ March 13, 2006: Message edited by: Keith Lynn ]
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Excellent Keith!!

I appreciate your programming skills!! I have not implemented it, however I am sure this gonna solve my problem...

Thank you soo much. This is a clear and logical approach..

I have a doubt here... suppose the condition which I am testing meets the first condition and then value of x is set to 5.. then the next set of condition statements will be false so the value of x will change because of these statements false statements right?

Eg:

x=valid(var,new BigDecimal(25,001.01),new BigDeciaml(50,001.01))?new BigDecimal(10.00):x;

Thank you.

[ March 13, 2006: Message edited by: Ayub ali khan ]
[ March 13, 2006: Message edited by: Ayub ali khan ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Recall the way that the ternary operator works.

x = (condition) ? value1 : value2;

Here if the condition is true, then the value of x will be value1. If the condition is false, the value of x will be value2.

So in the code that I gave, I made value2 the old value of x. So if the condition is true, the value of x will be replaced. Otherwise, it will stay the same.
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hi Keith,

You have resolved my issue. SOrry I could not pick the logic, as I implemented the code, I understood it and your explanation was simple

Perfect 10 points for you !!

There is one last condition which does not have an upperlimit.

Eg: Anything above var>=1050001.01 set x=45. How to accomodate this condition?

Thank you So much !!

Regards

Ayub
[ March 13, 2006: Message edited by: Ayub ali khan ]
Ayub ali khan
Ranch Hand

Joined: Oct 20, 2005
Posts: 380
    
    2
Hi Keith,

I implemented the last condition as simple condition..

val=(scaleval.compareTo(new BigDecimal(1050001.01))>=0)? new BigDecimal(45.00):val;

Thank you very much !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing the range of BigDecimal
 
Similar Threads
How to convert String to BigDecimal
finding an average
[absolute beginner] what do to with value objects
Comparable..
Get values from only child elements