File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing the range of BigDecimal

 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You could do something like this


[ March 13, 2006: Message edited by: Keith Lynn ]
 
Ayub ali khan
Ranch Hand
Posts: 394
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 394
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic