Skip to main content
18-Opal
Nov 14, 2019 10:14 PM UTC
Solved

BOM Table Relations: Convert String to Number (ston?)?

Previous erroneous Subject Heading: 2D Detailing / MBD: New Text and Symbol Fonts

--------------------------------------------------------------

Does anyone know of a good way to convert a string to a number so number operations can be performed?

For example, a simplified version of what I want to do is:

 

pn_end2=extract(asm_mbr_name,5,2)

/******e.g. pn_end2="12" (they will *always* be numbers but could also be something like 02)

pn_compare="07"

 

/*Convert to number using my made up string to number function ston (instead of ntos)

pn_end2_num=ston(pn_end2)

pn_compare_num=ston(pn_compare)

 

IF pn_compare_num<pn_end2_num

   <do something>

ELSE

   <do something else>

ENDIF

-------------------------------------------------

Any ideas on how to do this?

KenFarley
KenFarleyAnswer
21-Topaz II
Nov 15, 2019 12:07 PM UTC

Here's my bit of code that converts a string to a number. It's only for integers, so adding code for real numbers would be some additional fun to extract the numerals before and after the decimal marker. In the code, txtNumber is the string for the number, intNumber is the resultant integer.

intNumber = 0
numString = "0123456789"
index = 1
position = string_length ( txtNumber )

charDigit = EXTRACT ( txtNumber, position, 1 )
intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
IF position > 1
 index = index + 1 
 position = position - 1
 charDigit = EXTRACT ( txtNumber, position, 1 )
 intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
ENDIF

/* Repeat the IF--THEN block above as many times as you think you'll need, one for each
/* digit beyond the first.

 

8 replies

TomU
23-Emerald IV
Nov 14, 2019 10:48 PM UTC

PTC has been unwilling to do anything about this for as long as I can remember.  It would be so easy for them to just provide some industry standard type casting functions, but they won't.  Instead you're going to have to manually convert each string character to its corresponding number.

 

STRING1 = "37"
VALUE1 = -1

VAL = -1
CHAR = EXTRACT(STRING1, 1, 1)
IF CHAR == "0"
 VAL = 0
ENDIF
IF CHAR == "1"
 VAL = 1
ENDIF
IF CHAR == "2"
 VAL = 2
ENDIF
IF CHAR == "3"
 VAL = 3
ENDIF
IF CHAR == "4"
 VAL = 4
ENDIF
IF CHAR == "5"
 VAL = 5
ENDIF
IF CHAR == "6"
 VAL = 6
ENDIF
IF CHAR == "7"
 VAL = 7
ENDIF
IF CHAR == "8"
 VAL = 8
ENDIF
IF CHAR == "9"
 VAL = 9
ENDIF
IF VAL <> -1
 VAL1 = VAL
ENDIF

VAL = -1
CHAR = EXTRACT(STRING1, 2, 1)
IF CHAR == "0"
 VAL = 0
ENDIF
IF CHAR == "1"
 VAL = 1
ENDIF
IF CHAR == "2"
 VAL = 2
ENDIF
IF CHAR == "3"
 VAL = 3
ENDIF
IF CHAR == "4"
 VAL = 4
ENDIF
IF CHAR == "5"
 VAL = 5
ENDIF
IF CHAR == "6"
 VAL = 6
ENDIF
IF CHAR == "7"
 VAL = 7
ENDIF
IF CHAR == "8"
 VAL = 8
ENDIF
IF CHAR == "9"
 VAL = 9
ENDIF
IF VAL <> -1
 VAL2 = VAL
ENDIF

IF VAL1 <> -1 & VAL2 <> -1
 VALUE1 = ((VAL1*10) + VAL2)
ENDIF

 

Translate
24-Ruby III
Nov 15, 2019 8:39 AM UTC

Hi,

see https://community.ptc.com/t5/Routed-Systems/Getting-part-parameter-for-component-into-cabling/m-p/627912#M4499 

You can find there following solution

intNumber = 0
numString = "0123456789"
index = 1
position = string_length ( txtNumber )

charDigit = EXTRACT ( txtNumber, position, 1 )
intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
IF position > 1
 index = index + 1 
 position = position - 1
 charDigit = EXTRACT ( txtNumber, position, 1 )
 intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
ENDIF

/* Repeat the IF--THEN block above as many times as you think you'll need, one for each
/* digit beyond the first.

 

Translate
KenFarley
21-Topaz II
Nov 15, 2019 12:08 PM UTC

Hey, that was me. I wish I'd looked before posting my own code again....

Translate
KenFarley
KenFarley21-Topaz IIAnswer
21-Topaz II
Nov 15, 2019 12:07 PM UTC

Here's my bit of code that converts a string to a number. It's only for integers, so adding code for real numbers would be some additional fun to extract the numerals before and after the decimal marker. In the code, txtNumber is the string for the number, intNumber is the resultant integer.

intNumber = 0
numString = "0123456789"
index = 1
position = string_length ( txtNumber )

charDigit = EXTRACT ( txtNumber, position, 1 )
intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
IF position > 1
 index = index + 1 
 position = position - 1
 charDigit = EXTRACT ( txtNumber, position, 1 )
 intNumber = intNumber + 10^(index - 1) * ( SEARCH ( numString, charDigit ) - 1 )
ENDIF

/* Repeat the IF--THEN block above as many times as you think you'll need, one for each
/* digit beyond the first.

 

Translate
LawrenceS18-OpalAuthor
18-Opal
Nov 18, 2019 8:30 PM UTC

@KenFarley  & @MartinHanak,

 

Thank you very much for posting that very clever code.  I took some doing to even figure out what was happening and why it worked but I do understand it now.  I have successfully tested it and it works great...although I agree that PTC really needs to give us some extra commands as it could take one line instead of 30+ lines (depending on how many characters in the string that needs to be converted to a number.  Actually it would have been nice if Creo just recognized that it was a number when it first extracted it.

 

Thank you also @TomU  for posting the other method.  That was more efficient then the method I was attempting to do too prior to posting this.  Although I did find someone who created a code generator to do the job too.  I mention here to be comprehensive and because it is both brilliant and sad that a code generator was created to compensate for such a strong Creo relations deficiency.  Although like I said I am now using @KenFarley's method... above.

http://bl.ocks.org/veggielane/raw/449293ab850608d73a93/ 

Translate
TomU
23-Emerald IV
Nov 18, 2019 8:34 PM UTC

Glad to help.  That code of @KenFarley's is slick.  I'll definitely be using that in the future as well.  Always fun to learn new stuff!

Translate
LawrenceS18-OpalAuthor
18-Opal
Nov 15, 2019 5:25 PM UTC

I saw some great replies so thank you and will reply to those shortly.  however I noticed that I apparently posted this under the subject name which was supposedly 'restored' by the website before posting.  I will correct that now.  Sorry for the confusion but you guys still posted some really great answers!

Translate