Skip to main content
1-Visitor
May 19, 2017
Question

I need convert from decimal value to fraction value

  • May 19, 2017
  • 3 replies
  • 4999 views

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?

3 replies

15-Moonstone
May 19, 2017

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

21-Topaz II
May 22, 2017

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.

JAK1-VisitorAuthor
1-Visitor
May 23, 2017

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 ?

15-Moonstone
May 23, 2017

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

23-Emerald IV
May 25, 2017

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