Search code examples
crystal-reports

Convert String to Integer in Crystal Reports


I need to take a string which is actually an integer and use it as an index to a crystal reports array.

Global StringVar Array countryArray := ["UNITED STATES", "FRANCE", "ENGLAND", "CANADA"]

countryArray [ {myNumericField} ]  <-- how do I get this to be an integer, not float

{myNumericField} is actually a string, it comes from the database like that. I tried to convert it to a number. However toNumber() and val() do not work because the number Crystal creates is a floating point number. For instance if the string is "2" toNumber() & val() both make it 2.00 which cannot be used as an index to the array.

I tried to then do a toText but again, Crystal says the index of an array must be a number. How can I get {myNumericField} to be used as an index into the countryArray?

RESOLUTION After looking into it in depth, Crystal was throwing the index must be subscript of array error because one of the records was "0". Crystal arrays do not start with zero, they start with one. Both methods in the accepted answer work.


Solution

  • Do it like this:

    tonumber({myNumericField})
    

    This will return 2.00 as an axample:

    or

    INT(tonumber({myNumericField}));
    

    This will return 2 as an axample:

    This will allow you String to Number format.