10

I have a custom field called value__c in my custom object. This field can store values which could be a string, number, decimal or currency.

How can i check if this particular value can be converted to number,decimal or currency?

Thanks

14

You can use some of the new String methods, for example isNumeric.
Are you in a situation where you don't know in advance what kind of data will be in the string? You can use try and catch around attempted conversions, eg.

try {
  Integer x = Integer.valueOf(myString);
}
Catch (exception e) {
  // it's not an Integer, try something else
}
  • 6
    isNumeric for the win :) But it won't process decimals so proper order of try catching should be Decimal -> Long -> isNumeric -> give up, it's a string – eyescream Nov 6 '12 at 11:20
  • Nice to be able to use some of the new Winter13 String methods – Doug B Nov 6 '12 at 11:25
  • thats correct. I dont know which format it is, basically the field of string type but if there are numeric values then i would need to do some summing up. The design is a little crazy :) – Prady Nov 7 '12 at 7:13
6

You can use the Integer.valueOf and the instanceof constructs to achieve this (primitives also being objects in apex helps !)

    try{
    object  m = Integer.valueOf('123');
    System.debug(m instanceof integer);   //returns true if integer, false otherwise
    }
    catch(Exception e){
        // if not integer
    }
5

I don't know about Apex, but in most languages, try/catch blocks are not efficient to run (they have a lot of processor overhead). So if(test) methods are generally preferable to try/catch methods, especially inside a loop.

I know Apex doesn't have an isNumeric() method that recognizes the decimal point, but we can use the replaceFirst() method to remove just a single decimal point, then run isNumeric() on the remaining string:

if(myString.replaceFirst('.', '').isNumeric())
    myDecimal = decimal.valueOf(myString);
0

You can include the below class for your use. You can extend this for boolean, decimal. & datetime.

public class Convert{
    public class IntResult{
       public Boolean IsValid{ get; private set; }
       public Integer Value { get; private set; }
       public IntResult(Boolean pIsValid, Integer pValue){
           IsValid = pIsValid;
           Value = pValue;
       }
       public IntResult(Boolean pIsValid){
           IsValid = pIsValid;
           Value = null;
       }
    }
    public static IntResult toInt(String strInput){
        IntResult result = null;
        try {
            Integer intValue = Integer.valueOf(strInput);
            result = new IntResult(true, intValue);
        }
        Catch (exception e) {
            result = new IntResult(false);
        }
        return result;
    }
    static testmethod void testConvert_toInt() {
        IntResult res1 = Convert.toInt('34');
        if(res1.IsValid){
            System.debug(res1.Value);
        }
        IntResult res2 = Convert.toInt('we');
        if(!res2.IsValid){
            System.debug('Invalid Value');
        }
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.