Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- Creo Parametric
- :
- Detailing (MBD/ MBE)
- :
- Re: How To: Format a real number relation in a not...

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-08-2012
03:49 AM

08-08-2012
03:49 AM

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

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-08-2012
02:59 PM

08-08-2012
02:59 PM

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

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-08-2012
03:57 PM

08-08-2012
03:57 PM

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

Yep... the use of **Floor** was the "one very slight problem". Martin could've incorporated a test to do the rounding better. As it stands **12.98765 **rounds to **12.987 **when it should round to** 12.988.** A simple if/then/else test and the addition of the **CEIL** command could resolve this.

But that's just being nitpicky...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-08-2012
04:20 PM

08-08-2012
04:20 PM

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

Brian, I'd love to hear this from the "horse's mouth" so to speak...

Quite some time ago, I heard it was NASA that came up with the Round "5" to Even rule. Is this a fact?

n.nn5 round to n.nn(even) where n.nn(1-4) rounds down and n.nn(6-9) round up.

And where does that leave n.nn51 in relation to n.nn?

Its a great rule but the source has remained ambiguous. I'd love to confirm this with references.

Highlighted
##

Martin Hanák

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-09-2012
01:48 AM

08-09-2012
01:48 AM

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

Antonius,

of course you can use relations in any part. I thought the most easiest method for testing is using "new empty part".

To implement the conventional 1-4=down and 5-9=up "ROUND" principle, it is necessary to modify relations slightly, see below.

/* input variables

number_digits=3

x=12.98799**x=x+5/10^(number_digits+1)**

/* 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

Martin Hanák

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-09-2012
03:23 AM

08-09-2012
03:23 AM

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

Very clever!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-09-2012
03:35 AM

08-09-2012
03:35 AM

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

Yes indeed - adding 0.5 (or 0.0005, depending on the number of decimals you're rounding to) is a much neater technique than if/else. I've used that before with Excel's INT() function.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-09-2012
09:54 AM

08-09-2012
09:54 AM

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

Unfortunately, "the horse" can neither confirm nor deny that NASA is responsible or the rounding rule you mentioned.

One of my children pulled me aside the other day and said: "Dad... is there life on other planets?" Of course I said "We don't know yet but we're looking... and personally I believe there is life on other planets just by the sheer mathematical probability. I think we'll eventually find it."

This seemed to be a reasonable and, I hoped, thoughtful answer. When I asked why the child wanted to know, he said: "Well Uncle Danny (a huge redneck related to my **ex-wife**) said NASA already has proof aliens exist but they keep it secret from everyone. Is that true?"

**(Facepalm)**

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

08-09-2012
09:56 AM

08-09-2012
09:56 AM

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

Now he's just showijng off.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

12-14-2012
11:42 AM

12-14-2012
11:42 AM

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

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

Announcements

**Upcoming October 2019 Regional User Groups**

Middleburg Heights, OH Oct. 16th

Monroeville, PA Oct. 24th

Top Tags

- ptcuser_portal
- creo_parametric_proengi…
- drawing
- creo
- creo_system_administrat…
- creo_customization_proe…
- parametric
- modeling
- creo_machining_proengin…
- creo_parametric
- assembly
- pro/engineer
- bom
- creo_schematics_routed_…
- dimension
- table
- creo_2.0
- creo_view_visualization
- export
- pro/e
- design
- creo_elements_pro
- creo_simulate_proengine…
- wildfire5
- drawings
- cad
- creo drawing
- proengineer
- model
- wildfire