Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to number conversion Problem

Status
Not open for further replies.

edwindow

Programmer
Jul 13, 2001
51
0
0
US
Hi,
I want to do a simple conversion from a string format to a number format so I can do some caculations. I am using Crystal 8.5. Ok here is the problem.
I have two fields that are of type string; however, they have number variables that I need to do a caculation on that are within the string.
Here is the code I tried to use.

tonumber({security_profile.salary_code}[15]+{security_profile.salary_code}[16])

When I run the report, I get an error that says "the string is non-numeric." I would have thought since I used the tonumber conversion that it would have converted the value to a number. Am I missing a step?
 
Assuming that the numbers in [] brackets are the positions of the numerical parts of the string, try this:

tonumber(mid({security_profile.salary_code},15,1))+
tonumber(mid({security_profile.salary_code},16,1))

The mid() function returns a part of a string in the first line starting at position 15 and 1 character wide. You must convert both parts to numbers before adding them together.

Hope this helps
:)

Bill


 
You are trying to add 2 components of an array together, and THEN convert them to a number. I believe you need to convert both components together and then add them as
follows:

tonumber({security_profile.salary_code}[15])+
tonumber{security_profile.salary_code}[16])



Software Support for Macola, Crystal Reports and Goldmine
dgillz@juno.com
 
I tried both of these methods and I still get the error message "the string is non-numeric." I even tried doing with just one vs. adding them together.
 
Try this:

tonumber({security_profile.salary_code}[15]) + tonumber({security_profile.salary_code}[16])

This should work because you are first changing both fields to numbers before you add them together.
 
It sounds like not all of the fields have a number in the 15th and 16th locations.

You can check using:
if (isnumeric({security_profile.salary_code}[15]) and isnumeric({security_profile.salary_code}[16]))
then
tonumber({security_profile.salary_code}[15]) + tonumber({security_profile.salary_code}[16])
else
999999

Use the select expert to show only records that have a result of 999999 for the formula
Mike

 
I think you should give us an example or 2 of this security code as we are just guessing here...

kmcclung 's formula should work if these positions actually have numbers

tonumber({security_profile.salary_code}[15]) + tonumber({security_profile.salary_code}[16])

it would take only one value to be not numeric to cause the formula to crash

MY GUESS is that you are counting the first position as "0" and not "1" and you are out by one number in the position

In Crystal there is no zero element to an array they all start at 1

Hope this is the solution...Jim

 
ToNumber only works if the position contains 0-9. It will error otherwise. If you use VAL() instead of ToNumber, it is much more forgiving and will return a zero instead of an error. Ken Hamady, On-site Custom Crystal Reports Training & Consulting
Public classes and individual training.
Guide to using Crystal in VB
tek@kenhamady.com
 
Ngolem was right about one value being something other than a number. The report found an error at some point and returned nothing. I would have thought the report would have just overlooked that instance and continued. To fix this I used an IF-Then statement suggested above to resolve the issue. Thank you all for your help.

Eddie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top