Continue to Site

Welcome to MCAD Central

Join our MCAD Central community forums, the largest resource for MCAD (Mechanical Computer-Aided Design) professionals, including files, forums, jobs, articles, calendar, and more.

Relations

shankar_me

New member
Hi everyone,


I am having two parameters, A (real number, say 30.5) and B (String, say "val"). Now I want a third parmeter C (String) concatinating values A and B ("val30.5").When I went through all the functions available for string operations, I could find only one related function 'itos' which converts onlyinteger value to string. if a real value is passed, the function rounds it off to the nearest integer.


Regards,
Shankar
Edited by: shankar_me
 
I believe the answer will involve using a multiplier then parsing the string (extract) for integer and decimal values, concatenating with a "." There are examples here somewhere but (sorry) I can't remember the specific discussions.
 
You must first separate the number 30.5 into its two parts 30 and 5 like this.


B = "VAL"


A = 30.5


A1 = FLOOR(30.5) A1 = 30


A2 = A - A1 A2 = .5


IF A2 > 0/* FOR NUMBERS LIKE 30.5 */


NOTE = B + ITOS(A1) + "." + ITOS(A2*100) NOTE = VAL30.50


ELSE /* FOR NUMBERS LIKE 30 */


NOTE = B + ITOS(A1) + ".00"& nbsp;& nbsp; NOTE= VAL30.00


ENDIF


You'll notice that each component of the note must be added. Even the dot "." must be added separatly.The phrase ITOS(A2*100) will give you the string "50", if you want three decimal places just change 100 to 1000 to get "500" or multiply by 10 to get "5". The second function assumes you will sometimes get a whole number like 30 and you want to show a "0" after the dot. If you don't want to show a "0" after the dot leave this part out of your relations.


Remember to always break your note down into as small pieces as possible and then concatinate last.
 
I think that's 'slicker' than what I was trying to remember. Gonna copy the post this time.
smiley1.gif
 
Hi all,

Thank you for your suggestions.
In the way Jeff and Copyboy suggested, I have a limitation on the number of decimals to be displayed, i.e. the value should be approximated. But I dont want to approximate. I want to have a single logic using which I can convert a number having any number of decimals into a string value. In other words, if I specify A=30.125 in the above relation, I must get a value "val30.13" for C without changing 100 to 1000.

Regards,
Shankar
 
It seems you are now asking for relations that can ROUND OFF your numbers.


Do you need to have all decimal values rounded up to 2 decimals? i.e. .124 is .13 and .126 is .13?


Or is .124 rounded to .12 and .126 rounded to .13?


The relations for this can get very messy depending on what you want.
 
This code will test the fraction for the number of zeros with nested if statements.


B = "VAL"


A = 30.125


A1 = FLOOR(30.5) /*A1 = 30*/


A2 = (A - A1)*10000&nb sp; /*A2 = 1250*/


IF (A2/10) == CEIL(A2/10) /* TEST FOR ZERO IN TENS PLACE */
IF (A2/100) == CEIL(A2/100) /* TEST FOR ZERO IN HUNDREDS PLACE */
IF (A2/1000) == CEIL(A2/1000) /* TEST FOR ZERO IN THOUSANDS PLACE */
M = 1000
ELSE
M = 100
ENDIF
ELSE
M = 10
ENDIF
ELSE
M = 1
ENDIF


NOTE = B + ITOS(A1) + "." + ITOS(A2/M) /*NOTE = VAL30.125*/
 

Sponsor

Articles From 3DCAD World

Back
Top