Pro Engineer/Convert String to Number using Relations

Create parameters:

  • digitlist, type string
  • value, type real
  • digit_found, type integer
digitlist = "0123456789"

value = 0

/* because the search function returns the number of the item in the list
/* it is necessary to subtract '1' from the result; for example '0' will be position 1,
/* but the value should be one less than that e.g. 1-1=0; 2-1=1; 3-1=2.

/* the one's position is the end of the character string
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-0,1)-1
value = value+digit_found

/* the ten's position is one less than the end of the character string
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-1,1)-1
value = value+digit_found*10

/* the hundred's position should be obvious by now
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-2,1)-1
value = value+digit_found*100

/* the thousand's position
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-3,1)-1
value = value+digit_found*1000
d0=value

/*Without comments
digitlist = "0123456789"
value = 0
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-0,1)-1
value = value+digit_found
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-1,1)-1
value = value+digit_found*10
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-2,1)-1
value = value+digit_found*100
digit_found = search(digitlist,extract(rel_model_name(),len(rel_model_name)-3,1)-1
value = value+digit_found*1000
d0=value