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:
- Re: How To: Format a real number relation in a note?
- Transcript of real numbers to string
- Real to String in Relations
- help in combining parameters for drawing format
- Re: How To: Format a real number relation in a note?
- RE: Converting real numbers to string
- https://support.ptc.com/appserver/cs/view/solution.jsp?n=32366
- https://support.ptc.com/appserver/cs/view/solution.jsp?&n=108947

