cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Emerald II

## Converting Real Numbers to Strings

There are a plethora of discussions on converting real numbers to strings.  While PTC tech support does provide two working suggestions, they are not simple.  Beyond that, I have been unable to find anything on the PTC Community that will work in all cases.  (See list at bottom.)  With that as the background, here is my attempt at a robust real-to-string function that I believe works correctly for all positive numbers.  Please test this out and let me know if you can find any errors with it.

/* Real Number
X = 12.34567

/* Number of Digits
ND = 3

/* Rounded Number
RN = FLOOR((X+(5/10^(ND+1))),ND)

/* String Output - No Leading Zero
Y = ITOS(FLOOR(RN))+"."+EXTRACT(ITOS((RN-FLOOR(RN)+1)*10^(ND)),2,ND)

* To get a zero before the decimal point for values less than one, a conditional statement is required:

/* String Output With Leading Zero

IF FLOOR(RN) == 0

Y = "0."+EXTRACT(ITOS((RN-FLOOR(RN)+1)*10^(ND)),2,ND)

ELSE

Y = ITOS(FLOOR(RN))+"."+EXTRACT(ITOS((RN-FLOOR(RN)+1)*10^(ND)),2,ND)

ENDIF

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

For those who find this stuff interesting, what's unique about this formula is the addition of '1' to the decimal portion.  This effectively "traps" any zeros between the decimal and the last digit allowing them to be extracted as text.

.001      - Initial Value

1001     - Multiply by (10 * the number of digits)

"1001"   - Use the integer to string function to convert this to text.

"001"     - Extract everything except the first digit.

Thanks to ‌ for the rounding technique of adding '5' after the number of digits.   X+(5/10^(ND+1))

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

Related discussions and support articles:

10 REPLIES 10

## Re: Converting Real Numbers to Strings

Here are single line versions with fixed decimal places.  Just replace all the "X"s in each formula with your dimension.

```/* String Output - No Leading Zero, Three Decimal Places
Y = ITOS(FLOOR(X+.0005))+"."+EXTRACT(ITOS(((FLOOR((X+.0005),3))-FLOOR(FLOOR((X+.0005),3))+1)*1000),2,3)
```

```/* String Output - No Leading Zero, Four Decimal Places
Z = ITOS(FLOOR(X+.00005))+"."+EXTRACT(ITOS(((FLOOR((X+.00005),4))-FLOOR(FLOOR((X+.00005),4))+1)*10000),2,4)
```

## Re: Converting Real Numbers to Strings

Tom,

today is the day I've made a lots of things without knowing it

Are you talking about this SPR 2207900 – complain to PTC for BIG regression on Windchill 10.2 when citing me?

If that's the case, it's a different Marco and he's one of our consultants (he works for a PTC partner here in Italy).

Marco

## Re: Converting Real Numbers to Strings

Sorry, it was really late light night when I was working on this.  It should have said MartinHanak, and it was based on Re: How To: Format a real number relation in a note?

## Re: Converting Real Numbers to Strings

Don't worry Tom.

Marco
Participant
(in response to TomU)

## Re: Converting Real Numbers to Strings

I think I might be beating a dead horse on this one, but this might work for a wider array of real numbers.

str_y = extract(itos(floor(1000 * y )),1,string_length(itos(floor(1000* y )))-3) + "." + extract(itos(floor(1000* y )),string_length(itos(floor(1000* y )))-2,3)

## Re: Converting Real Numbers to Strings

Ok, this "itos" stuff is giving me a real headache.  I see a bunch of solutions, but I'm not understanding how to implement them, so I'll be specific.  I want to make the line below work and give me 3 decimal places fro all the parameters.  It's completely retarded that you can't simply use the "[.3]" that you can in dwgs.

Anyways, here's what I'm trying to do to get the relation to work to make the parameter "DWG_TITLE2" work to fill out the BOM correctly.  I've made it a flexible part, so it'd be nice to have it work parametrically to fill out the BOM.

DWG_TITLE2 = itos(A1_SPRING_OD) + " OD" + "," + " " + itos(A1_FREE_LENGTH) + " LONG," + itos(A1_WIRE_DIAMETER) + " WIRE DIAMETER,"

## Re: Converting Real Numbers to Strings

Post some sample values for your variables so we can see what a finished note should look like.

A1_spring_od

A1_free_lemgth

A1_wire_diameter

Also how many decimal places do you want in your note? That will change how the ITOS has to be manipulated.

## Re: Converting Real Numbers to Strings

Hey Ben!

I want 3 decimal places to the right in each of the parameters.  So, it should read in the BOM:

"1.000 OD, 2.000 LONG, .147 WIRE DIAMETER,"

I can't imagine us using springs with a wire diameter 1" or over, so just the 3 digits to the right would suffice on that one.  We WILL however need springs both under and over an inch in OD, and under and over an inch in length.  If that makes things insanely complicated, I'll just make that "DWG_TITLE2" parameter flexible and force the user to manually put the info in.  We have some new Pro/E users here and I

m trying to make the library as easy as possible for them.  😉  This is the first time I've played with this and, man, it seems to be a total PITA.....

Thanks!

Frank

Regular Member
(in response to TomU)

## Re: Converting Real Numbers to Strings

thanks for the described method, TomU, it is very helpful.

Is it wrong for me to try to use it in a drawing program?

I would like to set a note on a drawing to either " " or to "surface hardened 0.1 - 0.2 inches" depending on a parameter (basically until the assembly is a work in progress, it should stay at " "). I have copied the formulae to the drw program, but it gives me an error every time i try to edit it again (apparently it doesn't like RN:d, X:d drawing parameters).

I am doing this because this is for main assembly drawing only and i would like to keep it separate from the production drawings just in case. Also i already have a plethora of model params, some of them with similar names to the ones above, but almost none on the drawing (drw parameter).

## Re: Converting Real Numbers to Strings

I don't think it will work in a drawing program.  Instead of using a drawing program use a single cell repeat region and use it in the repeat region relations.

Announcements