Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

Integer to BigDecimal

hi guys
i have something like this in my value object CriteriaVO

private Object values[] = null;
public Object[] getValues() {
            return values;
      }

public void setValues(Object[] values) {
            this.values = values;
             
            for (int i=0; values!=null && i<values.length; i++){
                  if (values[i] !=null && values[i] instanceof String)
                        this.values[i] = ((String)values[i]).toUpperCase();
            }

      }

 
AND i have a method like this which uses the value object:

private static Search  modify(CriteriaVO criteria[] )
for (int j = 0; j < criteria.length; j++)
{
if((!criteria[j].getValues().equals(Plan.getPeak())))       //this is not matching
}

//criteria[j].getValues()  is 1000
and Plan.getPeak()  is of type Big integer and its value is 1000.00

so i want to convert criteria[j].getValues() (which is 1000)  to Bigdecimal so that the values match.

any idea how i can do that
thanks
J
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The getValues you've shown returns an array, so how can it be 1000?
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or BigDecimal - whichever
                       this.values[i] = new BigInteger((String)values[i]);
Assuming when an Object is String it's not a number (else why convert it to uppercase)

if (values[i] !=null && values[i] instanceof String) {
      // whatever
}
else if (values[i] !=null && values[i] instanceof Number) {
      BigDecimal bi = new BigDecimal(values[i].toString());
}
Avatar of jaggernat
jaggernat

ASKER

is this correct
(!BigInteger((String)values[i]).equals(pricePlan.getPeakAllowanceMinutes()))

it shows an error:
It says method BigInteger is undefined for Type Plan class
my if statement is
if(!BigInteger((String)values[i]).equals(pricePlan.getPeakAllowanceMinutes()))
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the String types in the array contain numbers, why are you converting to uppercase?:

>>this.values[i] = ((String)values[i]).toUpperCase();
it could be anything , numbers or alphabets
> it could be anything , numbers or alphabets

then you'll need to add a try/catch block around what i suggested above to habdle cases where it is not a number (unless you have some other way of telling)
Or first check if it numberic or not?

you can do that with a regular expression

http://www.objects.com.au/java/qa/1260329489.html

or loop thru the chars checking the digits (preferred)

let me know if u have any questions
thanks
:-)