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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Converting Real Numbers to Strings

TomU
23-Emerald IV

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

1.001    - Add '1'

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:

21 REPLIES 21
TomU
23-Emerald IV
(To:TomU)

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)

Marco_Tosin
21-Topaz I
(To:TomU)

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
TomU
23-Emerald IV
(To:Marco_Tosin)

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?

Marco_Tosin
21-Topaz I
(To:TomU)

Don't worry Tom.

Marco
tdowney
10-Marble
(To:TomU)

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)

Patriot_1776
22-Sapphire II
(To:tdowney)

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,"

 

Thanks in advance!

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.

Patriot_1776
22-Sapphire II
(To:BenLoosli)

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

rmcboaty
10-Marble
(To:TomU)

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).

TomU
23-Emerald IV
(To:rmcboaty)

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.

TomU
23-Emerald IV
(To:TomU)

Creo Parametric 11.0 was released today, and it includes a new 'rtos' function to perform 'real to string' conversion automatically.

https://support.ptc.com/help/creo/creo_pma/r11.0/usascii/index.html#page/whats_new_pma/fund_real_to_string.html#

SikiF
10-Marble
(To:TomU)

Congratulations, the rtos function is finally here in Creo Parametric  11.

However, I am still missing some functionality in this regard.

How can I remove the trailing zeros in the rtos function (eg. rtos (22.500) to get string with value "22.5")?

Has anyone elese encountered this problem?

BenLoosli
23-Emerald II
(To:SikiF)

I just tested this.

In the relation editor:

c=2.2500
d=rtos(c)
e=rtos(22.5000)

In parameters, I have:

BenLoosli_0-1720018869533.png

 

SikiF
10-Marble
(To:BenLoosli)

Yes, that's right, BenLoosli. I'm sorry. I was very imprecise.

The problem I see with trailing zeros is when you limit the number of decimal places, in this case let's say 3 decimal places. Then you get 2.250 instead of 2.25 or try to limit to 4 decimal places then you get 2.2500 instead of 2.25. etc.

SikiF_0-1720066524386.png

 

Well, the funny thing is, if you write an integer into the rtos function, you always get a result with one decimal place and a trailing zero. rtos(123) ==> 123.0

 

I think it would be much better if there was also a third argument to control trailing zeros: like rtos(123, 3, 0).

Meaning:

- the first argument "123" is a real number,

- the second argument "3" is the number of decimal places, (optional)

- the third argument "0" or "1"; where  "0" does not include a trailing zeros and "1" does include a trailing zero. (optional default is 1)

 

Patriot_1776
22-Sapphire II
(To:SikiF)

All interesting and very useful stuff (thanks!), but, I think the best solution would be, is there a way to truncate the result after the last NON-ZERO digit to the right of the decimal?  Any ideas?

I have found syntax of rtos function:

rtos(real)
Converts real numbers to strings. Here, real can be a parameter, dimension, or a decimal number.
For example,
A=rtos(123.456789) => A= 123.456789
B=rtos(123.456789,3) => B= 123.457
C=rtos(123.456789,4,yes) => C= 1.2346e02
A, B, and C are string parameters.

 

The third argument is already taken by converting the real number to scientific mode.

 

However, people's needs are different.

There are always some people who want trailing zeros and some who want no trailing zeros. Like in your case where you want to have no leading zero. So I think PTC should also consider including an option to remove trailing zeros in the rtos function.

 

Any idea, comment on this topic?

Patriot_1776
22-Sapphire II
(To:TomU)

'Morning Tom!

 

Just stumbled across this.  Since we're on Creo 8 and can't test it, does anyone know what rtos does (in my case in a family table) where there is a "0" to the left of the decimal point (i.e. 0.360)?  This would be really handy for me IF the value showed up in the string as ".360" since I can't use the "0" to the left in my BOM description of things.  If I get the "0" it's pretty useless.  Although by the time we finally get to Creeo 11 I'll probably have long since retired...LOL

 

Thanks for posting this!

 

F

Hey Frank,

Sorry, it keeps the leading zero, but drops trailing zeros.

rtos(0.360) yields a string of '0.36'.

 

Patriot_1776
22-Sapphire II
(To:BenLoosli)

'Sup Ben!  Thanks for testing that!

 

Drat!  Man, that is a bummer though.  Maybe there's a cure to delete that with some simple coding...which is of course WAY over my particular head...

 

Thanks again, and happy 4th of July!

Simple code

 

a=rtos(0.360)
if extract(a,1,1) =='0'
b=extract(a,2,3)
else
b=a
endif

 

a=0.36

b=.36

 

You can get more complex with things like string length to get the length of the string 'a', then replace the 3 in the extract statement with the variable for the length of the string depending on the number of digits in your initial value.

Patriot_1776
22-Sapphire II
(To:BenLoosli)

Wow, I totally suk at coding, so, none of that makes sense to me but I can see the results.  So, if we ever get on Creo 11 before I retire, I'll have to have some of the guys here try that.

 

Thanks, and have a great 4th of July, because....'Merica!

 

F

Announcements


Top Tags