0

I am trying to perform a quick fix for a client. We have a report field that shows tolerance values in strings like +/-20 , -19 +18. This is in micrometer and the client wants it in millimeter. So i need to divide only the numeric part of this string by 1000 and display the result.

I am relatively new to crystal reports, with my limited knowledge and from searching this site for suggestions, i created a function with the following code lines,

Function (stringvar x)

local stringvar array input := split(x,"+/-");

val(input[ubound(input)])/1000

The above function works perfectly for tolerance values of +/-. However i am not able to figure out a way to do it for '-19 +18'. I would like to have the result as -0.019 +0.018

I can easily do it in the database source and send it to the report. However the client needs a quick fix of just the report. Any help would be greatly appreciated.

  • Please, reread your question and fix it - if you process your project specifications in same way, then it is no surprise that some spaceships fall. I mean this contradiction: "+-20 is in millimeters and the client wants in micrometers" - this needs (per statement) to multiply with 1000, giving result as 20'000. Your code divides by 1000 and gets 0.020. Which interpretation is correct? (I can guess, but not all cases are so simple.) – Arvo Jan 22 '15 at 13:35
  • Thanks Arvo for pointing out the mistake(irrelevant) in my question. However, I believe that information does not have any connection with the solution I was trying to arrive or arrived. My request here to the experts was mainly to find out how to parse through the strings and extract the numeric values so that I can perform my conversion(mm to microMeter or vice versa is irrelevant). – Vijay Raghavan Jan 22 '15 at 13:54
  • I appreciate your effort to help out with a solution. However all I got from your post here is to teach me to correct an irrelevant part of the question, conversion between mm to micro meter and finally basic math of division and multiplication. So, probably before commenting next time my humble request to you would be to refrain from commenting on the other person's ability to process the project specifications without actually being of any help. Especially when the question has already been answered, Thank you, have a good day. – Vijay Raghavan Jan 22 '15 at 13:58

2 Answers2

0

try this

if(Left (x, 3 )="+/-")
then ToNumber(split(x ,"+/-")[2])/100
else if(Left ({x , 1 )="+")
then ToNumber(split(x ,"+")[2])/100
else if(Left (x , 1 )="-")
then ToNumber(split(x ,"-")[2])/100
Siva
  • 9,043
  • 12
  • 40
  • 63
  • Thanks for your answer Siva. Unfortunately this does not solve the problem I am having when the string contains both + and - as in "-19 +18". It would work if the strings are '+/-19', '-19', '+18'. The main requirement I have is to deal with the string '-19 +18'. I can try to work with what you have given here and see if I am able to arrive at a solution. – Vijay Raghavan Jan 22 '15 at 09:52
  • will there be any space between string `-19 +18` – Siva Jan 22 '15 at 11:35
  • Yes, there are two spaces in between the strings. I tried using mystr[2 to 3] and mystr[7 to 8] . I am able to get the result that I want. However if the string length varies it wouldn't work. For the time being I see this as a solution. – Vijay Raghavan Jan 22 '15 at 12:03
  • I used ToNumber(mystr[2 to 3])/1000; to get the value and divide it by 1000. The answer should be 0.019 but its always rounded up as 0.020. Any idea how to solve it? – Vijay Raghavan Jan 22 '15 at 12:12
  • I fixed this rounding up by using ToText(x,3). I did not know by default it takes the number of decimal points to 2. When I increased it to 3 it works perfectly. – Vijay Raghavan Jan 22 '15 at 13:00
0

I figured out an answer which would work for this specific case. I used the following conditions in a formula field. Based on the condition I called a User defined function 'Tolerance','Tolerance2', 'Tolerance3'.

Formula field:

IF (Left ({PDPRINTDATA.PD_T45}, 3 )="+/-") THEN
'+/-' + ToText(Tolerance({PDPRINTDATA.PD_T45}),3)
ELSE IF (Left ({PDPRINTDATA.PD_T45} , 1 )="-") THEN
'-' + ToText(Tolerance2({PDPRINTDATA.PD_T45}),3) + '  +' + ToText(Tolerance3({PDPRINTDATA.PD_T45}),3)

Tolerance:

Function (stringvar x)
local stringvar array input := split(x,"+/-");
val(input[ubound(input)])/1000;

Tolerance2:

Function (stringvar x) local stringvar mystr := x; 
If NumericText (mystr[2 to 3]) Then
 ToNumber(mystr[2 to 3])/1000
Else 0

Tolerance3:

Function (stringvar x)
local stringvar mystr := x;
If NumericText (mystr[7 to 8]) Then
ToNumber(mystr[7 to 8])/1000
Else
0

This solution works for me considering the fact that the string will always be of this format '+/-XX' or '-XX +YY'.