Convert Decimal to Fractions

Div by 12 for inches
Mod by 12 then div remainder by desired denominator ?

Doing this in SLC 5/05 what instructions could I use?

Trying to turn the floating point number into feet and inches and trying to handle the inches down to the 16th at least but maybe good to go ahead and set it up by the 32nds.
 
Essentially, you need to reverse the fractional to decimal conversion. Multiply the decimal value by 32. That result rounded will yield the numerator.
Ex: .437 x 32 = 13.98. Round to 14. 14/32 is your fractional value. Haven't thought how to reduce to lowest common denominator (7/16) yet.
 
For 16th's, 16 lines of code, starting with smallest value first. Same as a lookup table.
If x > .03125 (1/32) then HMItext = "1/16"
If x > .09375 (3/32) then HMItext = "1/8"
If x > .15625 (5/32) then HMItext = "3/16"
etc.
The 32nds in parentheses isn't part of the formula. I put that in to show how I determined midpoint to round up.
Need someone else to show this in AB.
 
What I'd probably do, if someone told me that I had to, would be to build a UDT with a Real (".Frac"), and then however I wanted to display the fraction (String; or Numerator (DInt) & Denominator (DInt).

Then I'd make a UTD Array tag (size 33 or 17), and populate the table as keithkyll suggested, with 0.0 in UDT[0].Frac; .03125 (1/32) or .0625 (1/16) in UDT[1], etc.

Then I'd strip out the decimal (MOV Real DInt; Frac = Real - Dint; If Frac < 1, Frac = Frac + 1.)

Then a simple FSC (Frac >= UDT[Ctr.POS].Frac) to find the right record, and indirect MOV the display part into the actual display registers.

Not sure how best to handle the case where there is no fraction (1.0 in): show a blank string, or "0/2", "0/32" or ???

But that's me. Better to just train your operators to know their decimals or bring them into the 19th century with metric.

Cheers,
 
Here is a quick basic routing you can convert it to structured text:

' Data structure definitions
TYPE Fraction
Num AS LONG
Den AS LONG
END TYPE

' ************************************************
' ** Name: Fraction2String$ **
' ** Type: Function **
' ** Module: FRACTION.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Converts a type Fraction variable to a string.
'
' EXAMPLE OF USE: fa$ = Fraction2String$(a)
' PARAMETERS: a Structure of type Fraction
' VARIABLES: (none)
' MODULE LEVEL
' DECLARATIONS: TYPE Fraction
' Num AS LONG
' Den AS LONG
' END TYPE
'
' DECLARE FUNCTION Fraction2String$ (a AS Fraction)
'
FUNCTION Fraction2String$ (a AS Fraction) STATIC
Fraction2String$ = STR$(a.Num) + "/" + STR$(a.Den)
END FUNCTION


' ************************************************
' ** Name: String2Fraction **
' ** Type: Subprogram **
' ** Module: FRACTION.BAS **
' ** Language: Microsoft QuickBASIC 4.00 **
' ************************************************
'
' Converts a string to a type Fraction variable.
'
' EXAMPLE OF USE: String2Fraction f$, a
' PARAMETERS: f$ String representation of a fraction
' a Structure of type Fraction
' VARIABLES: (none)
' MODULE LEVEL
' DECLARATIONS: DECLARE SUB String2Fraction (f$, a AS Fraction)
'
SUB String2Fraction (f$, a AS Fraction)
a.Num = VAL(f$)
a.Den = VAL(MID$(f$, INSTR(f$, "/") + 1))
END SUB
 
Last edited:
I did this way.
One problem is if the Raw Number is odd then PLC rounds up. If the Raw Number is Even then the PLC rounds down. 32.5 = 32 and 33.5 =34

Dividing the decimal by .03125 will give you the number of 32nds. Then to reduce to the smallest fraction you look at the binary bits of that number. If bit 0 is true then the number is odd and cannot be reduced any further. If bit 0 is false then you see which binary bit is true to get you divisor.




Fraction012.PNG
Fraction3-7.PNG
Fraction8.PNG
 
Yet another implementation, in the SLC instruction set, but untested since I don't have a SLC at my disposal. Not intended for negative decimal numbers, though could be made to work if necessary.
FRAC_01.jpg

FRAC_22.jpg

FRAC_33.jpg

FRAC_45.jpg
 
Last edited:

Similar Topics

Hi all! looking for help converting an 8 bit word SINT tag into decimal. I have an IFM SBN246 flow sensor wired in my controller (L310ER) and it...
Replies
4
Views
2,129
Hey All, i have been fortunate enough to play around with an Applied Motion Products Servo Drive SV200 with 100 watt/ 24vdc congif. I managed...
Replies
2
Views
1,858
Hey all, Here's a tricky decimal to binary conversion project I could use some help with: In my RSLogix 5000 application, I have a "checklist"...
Replies
7
Views
3,025
Hey all, I've reviewed many threads regarding converting one data type to another and I have something I'm working on that I'm stumped on and...
Replies
9
Views
5,863
I'm working with SLC 500 Data File. I have a few numbers like 8.313452e+07 . I forget , how do you convert these numbers . Thanks so much.
Replies
11
Views
2,429
Back
Top Bottom