Skip to main content
17-Peridot
August 1, 2012
Solved

How To: Format a real number relation in a note?

  • August 1, 2012
  • 5 replies
  • 34296 views

I have a real number variable and I want to represent this in a note with a limited number of decimal places.

The default decimal places is 3 in the part file and that is what I want. The note I want needs to be limited to 1 decimal place.

Since this is not a "dimension", I cannot edit the number of decimal places in properties.

I can set the decimal places in the "options" of the measure:distance dialog (for instance) but the relation only follows the units set in options of a saved measure, not the decimal places set in the options.

Creo 2.0

thanks

Best answer by James62

hi Antonius,

I would ask PTC support the very same question but I can't.

Anyway PM me if you need some help with this I had it figured out before buying Creo.

And please vote for this one: http://communities.ptc.com/ideas/1400#

EDIT: Just an idea. If your parameter is a real number then you could try adding &param_name[.1] to your drawing note. I think just that could solve your problem. So there might not be a need to convert real to string if you aren't gonna add additional chars and use this in repeat region.

5 replies

James621-VisitorAnswer
1-Visitor
August 2, 2012

hi Antonius,

I would ask PTC support the very same question but I can't.

Anyway PM me if you need some help with this I had it figured out before buying Creo.

And please vote for this one: http://communities.ptc.com/ideas/1400#

EDIT: Just an idea. If your parameter is a real number then you could try adding &param_name[.1] to your drawing note. I think just that could solve your problem. So there might not be a need to convert real to string if you aren't gonna add additional chars and use this in repeat region.

17-Peridot
August 2, 2012

Thanks Jakub.

Where is there a good comprehensive list of all the functions available in Creo?

1-Visitor
August 2, 2012

You're welcome.

Do you mean part/assy relations functions? Or comprehensive list of all the functions available in Creo?

I don't know but Brian maybe does. http://communities.ptc.com/message/186647#186647

The site I've learned most of the relation functions from isn't available anymore.

24-Ruby III
August 8, 2012

Antonius,

just FYI you can convert REAL value into STRING value using the following relations in new empty part.

/* input variables

number_digits=3

x=12.98765

/* output variables

t1=floor(x,number_digits)

t2=itos(t1*10^number_digits)

t3=extract(t2,1,string_length(t2)-number_digits)+"."+extract(t2,string_length(t2)-number_digits+1,number_digits)

Martin Hanak

13-Aquamarine
August 8, 2012

Martin's getting sneaky trimming the digits as a string instead of a real number. This is a great trick... if only more people understood some of the nifty stuff you can do with relations. Unfortunately it's one of those rare arts that few seem to know about.

Martin is really giving a goldmine of information here but you have to dig deep to fully appreciate it.

The four functions Martin is using are:

  • FLOOR - rounds any real number down to the next closest value with the number of digits specified. The format is floor(real number,number of digits to round to)
  • ITOS - the "integer-to-string" function. The format is just itos(integer).
  • EXTRACT - grabs a portion of a string. The format is extract(string, start position, number of chars to extract)
  • STRING_LENGTH - provides the length of a string in characters. The format is string_length(string)

But you really have to examine the code to truly see the beauty here.

The itos function only converts integers to strings. It cannot handle decimal values (real numbers). If you wrote x = itos(12.98765) you'd end up with x="12". The decimal values would be discarded. Therefore, Martin's relations incorporate a thoughtful bit of math to handle this problem.

First, he sets the variable number_digits to 3. Then, first set a variable for the number digits after the decimal to preserve. He uses the floor function to round down the real number x which starts at 12.98765. Once this function has completed it's work, x is 12.987.

Next, Martin uses the itos function. He multiples 12.987 by 10 to the power of 3 (or 1000) to remove the decimals. Thus, once itos has done it's work, the value has changed from 12.987 to "12987" (now a string with the decimal removed)

In the last line of the relations, Martin uses extract and the string_length functions together. This final line writes out the first two characters of the string "12987" which are just "12". Next a dot (.) is written out. Finally, the last 3 characters of the string ("987") are written. Together this is "12.987" which completes the conversion from 3 digit real number to string".

The whole thing is very, very clever. Thanks for posting this Martin.

Thanks!

-Brian

PS: There's one very slight problem though... I'll leave it to any curious readers to figure it out. Here's a hint... it has to do with the "6" in the original value of x which was 12.98765 Maybe someone will catch it and demonstrate the fix.

17-Peridot
August 8, 2012

Oh, I definitely appreciate Martin's approach. In the day, I was very into programming DBASE III and IV and completely understand the approach and even the syntax.

The use of "FLOOR" might be a bit troubling since a "ROUND" should be available to round to either the "even principle" or the conventional 1-4=down and 5-9=up. But even thsat could be "relationed" to resolution.

Why a "new empty part"?

This stuff has to be written somewhere; No?

7-Bedrock
December 14, 2012

I just caught this thread and was a little excited to see if PTC had actually added a much needed function. But alas .... no .... just another verification that they don't listen to the users.

We have been using this "trick" to convert parameters that are real numbers to notes in drawings since I started. The function we have asked for is RTOS (Real to String) with a way to control the number of decimals.

The actual answer to the posted question is .... there is no direct method .... just Martin's "trick".

1-Visitor
December 18, 2012

Funny how the "integer to string" (ITOS) function has been with us for so many years, but still "no real to string" function in sight.

My guess is that it would take PTC a few hours to program this function into Creo, still we have to create pages of cryptic relations to perform such a simple task.

What a monkey to do?

Lets hope PTC gets there act together and fix this issue once and for all.

And when they are at it, why not provide the entire library java functions related to managing strings and numbers.

Now, that would something!!!

10-Marble
August 5, 2013

I have just found this thread by looking for something else.

This is how I do it:

/*#####Number to string for given decimal places (up to 9)#####

/*#####ASSIGN DIMENSION BELOW TO A VARIABLE#####

NUMBER=d8

/*#####ENTER DECIMAL PLACES YOU WANT TO SHOW#####

DECIMAL_PLACES=3

/*CALCULATION (REPLACE “X” WITH NUMBER 1,2,3,…FOR EACH DIMENSION YOU WANT TO MAKE AS STRING)

NUMBER_INT=FLOOR(NUMBER)

NUMBER_DEC=NUMBER-NUMBER_INT

NUMBER_DEC=NUMBER_DEC*10^(DECIMAL_PLACES)

X_STR_NUMBER_INT=ITOS(NUMBER_INT)

CHECK=(NUMBER_DEC-FLOOR(NUMBER_DEC))

IF (CHECK<0.5)

X_STR_NUMBER_DEC=ITOS(FLOOR(NUMBER_DEC))

ELSE

X_STR_NUMBER_DEC=ITOS(CEIL(NUMBER_DEC))

ENDIF

/*NUMBER AS A STRING "STR_NUMBER" WITH GIVEN DECIMAL PLACES

X_STR_NUMBER=X_STR_NUMBER_INT + "." + X_STR_NUMBER_DEC

This macro also rounds up or down the value for given decimal places.

1-Visitor
August 17, 2013

Nice solution, Danilo.

10-Marble
August 17, 2013

When nothing else is available from PTC this is the best I can do. It took me 5 minutes to make this script. I do not understand why PTC could not do something like this? They should be better in this, right? If Excell has VAL and STRING functions since forever, I do not understand why Creo does not have it in 21st century?

1-Visitor
January 7, 2014

Simply adding number format could solve it ?

&MASS:FID_9.1 is the parameter included in the note

Mass: &MASS:FID_9.1: gr -> Mass : 125.326 gr -> print 3 decimal (default)

Mass: &MASS:FID_9:1[.1] gr -> Mass : 125.3 gr only -> print 1 decimal only.

23-Emerald IV
June 26, 2015

This is not for drawings, it's for getting the real number converted to a string for use as a model parameter.  For example, if you want a DESCRIPTION parameter (in the model!) to include multiple three place dimensions, you have to manually do this conversion before building the parameter text.

/* Real Numbers (map to actual dimensions)
L1 = 1.25
L2 = 2.50
L3 = 3.00

/* String Output - No Leading Zero, Two Decimal Places
L1_STRING = ITOS(FLOOR(L1+.005))+"."+EXTRACT(ITOS(((FLOOR((L1+.005),2))-FLOOR(FLOOR((L1+.005),2))+1)*100),2,2)
L2_STRING = ITOS(FLOOR(L2+.005))+"."+EXTRACT(ITOS(((FLOOR((L2+.005),2))-FLOOR(FLOOR((L2+.005),2))+1)*100),2,2)
L3_STRING = ITOS(FLOOR(L3+.005))+"."+EXTRACT(ITOS(((FLOOR((L3+.005),2))-FLOOR(FLOOR((L3+.005),2))+1)*100),2,2)

/* Generate DESCRIPTION parameter
DESCRIPTION = L1_STRING + " x " + L2_STRING + " x " + L3_STRING

23-Emerald IV
June 26, 2015

Sorry, just realized I replied to a very old post.    The answer you provided was acceptable since the OP was using a note.