Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
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?
Solved! Go to Solution.
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.
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
Hi,
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.
Hey, that was me. I wish I'd looked before posting my own code again....
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.
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/
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!
Aw, thanks, everyone.
The ability to use relations to define models is one of my favorite things about Creo, something I see as lacking in some of its competitors. I just wish they'd put in a wee bit of effort and add simple functions like this. I'd be more than happy to write the thing for them. For a standard language (C, Java, Perl, etc.), this kind of thing is often assigned in beginner classes, with extra credit for handling exponential (i.e. 1.23E-04) numbers and the like.
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!