I need convert from decimal value to fraction value
For example;
Parameter (or) family table instance "A" decimal value 0.75..i need to convert fraction value (3/4).
What is the relation to convert Parameter "A" from decimal value to fraction value?
You can use this algorithm Convert Decimals to Fractions
You can chceck reminder after dividing but you cant determine number of needed divisions because there are no loops in Creo relations.
I think you have to use any API to solve your problem.
For example in jlink (using java): java - Converting decimal to fraction - Stack Overflow
Well, that would depend on what you are working with. Is this a Creo relation? A programming language like C or Java, etc.?
What are you trying to get, just any fraction? Is 13/71 okay, or are you trying to get fractions that are the "common" ones for inch measurements, like 1/2, 1/4, 3/8, 1/16, etc. which are using denominators that are all multiples of 2?
The complexity of the algorithm that would be suggested is highly dependent on what your end result needs to be.
Yah,is this creo relation.If i give any decimal value,it need to convert fraction value.
for example:
In my A value 0.75.
I need fraction of this (3/4) convert into parameter.
If i need to be fraction value into parameter,what i do ?
You cant do this with relations. What you can do is make this fraction 75/100 and cast it to string but there is no way to gain 3/4 with relations. You will have to do it manually or use API (programming).
Hi,
if your list of decimal values is limited to 0.25, 0.5, 0.75 then you can use relations:
if A==0.25
B="1/4"
endif
if A==0.5
B="1/2"
endif
if A==0.75
B="3/4"
endif
MH
Again, you have not specified the format of your needed output. So, this is a relatively simple method I use to get "standard" fractional sizes for bolts and things from decimal numbers (64ths, 32nds, etc.) If that's not what you want, you are going to have to be more specific about what you are actually asking.
----- [ Begin relations code ] ------
/*
/* Convert the real number "realvalue" to the nearest fraction,
/* limiting our denominator to multiples of 2, like 2, 4, 8, 16, etc.
/* The result is stored in "txtfraction", which is a STRING.
/*
txtfraction = ITOS ( floor ( realvalue * 64 ) ) + "/64"
IF MOD ( floor ( realvalue * 64 ), 2 ) < 1
txtfraction = ITOS ( floor ( realvalue * 32 ) ) + "/32"
IF MOD ( floor ( realvalue * 32 ), 2 ) < 1
txtfraction = ITOS ( floor ( realvalue * 16 ) ) + "/16"
IF MOD ( floor ( realvalue * 16 ), 2 ) < 1
txtfraction = ITOS ( floor ( realvalue * 8 ) ) + "/8"
IF MOD ( floor ( realvalue * 8 ), 2 ) < 1
txtfraction = ITOS ( floor ( realvalue * 4 ) ) + "/4"
IF MOD ( floor ( realvalue * 4 ), 2 ) < 1
txtfraction = ITOS ( floor ( realvalue * 2 ) ) + "/2"
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
------ [ End relations code ] ------
Here is a chunk of code I've used in the past. This will go down to 1/16ths and then fall back to a decimal value. Just replace asm_mbr_l1 with your dimension or parameter name.
/* ==================================================
/* ====================CONVERT L1===================
/* ==================================================
NUM = asm_mbr_l1 * (1/25.4) /* Convert from mm to inches
NUM = ((CEIL(NUM*16))/16) /* OPTIONAL - Round up to nearest 1/16"
STR = ""
INT = FLOOR(NUM)
FRAC = FLOOR(((NUM-INT)+(5/10^5)),4)
IF FRAC == FLOOR(FRAC) /* Not fractional
IF INT > 0
STR = ITOS(INT)
ENDIF
ELSE
IF (FRAC*2) == FLOOR(FRAC*2) /* 1/2
IF INT == 0
STR = ITOS(FRAC*2) + "/2"
ELSE
STR = ITOS(INT) + "-" + ITOS(FRAC*2) + "/2"
ENDIF
ELSE
IF (FRAC*4) == FLOOR(FRAC*4) /* 1/4
IF INT == 0
STR = ITOS(FRAC*4) + "/4"
ELSE
STR = ITOS(INT) + "-" + ITOS(FRAC*4) + "/4"
ENDIF
ELSE
IF (FRAC*8) == FLOOR(FRAC*8) /* 1/8
IF INT == 0
STR = ITOS(FRAC*8) + "/8"
ELSE
STR = ITOS(INT) + "-" + ITOS(FRAC*8) + "/8"
ENDIF
ELSE
IF (FRAC*16) == FLOOR(FRAC*16) /* 1/16
IF INT == 0
STR = ITOS(FRAC*16) + "/16"
ELSE
STR = ITOS(INT) + "-" + ITOS(FRAC*16) + "/16"
ENDIF
ELSE
FRAC = FLOOR(((NUM-INT)+(5/10^4)),3)
IF INT == 0
STR = "."+EXTRACT(ITOS((FRAC+1)*10^3),2,3)
ELSE
STR = ITOS(INT)+"."+EXTRACT(ITOS((FRAC+1)*10^3),2,3)
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
L1_STR = STR