This board is for PLC Related Q&A ONLY. Please DON'T use it for advertising, etc. 

Try our online
PLC Simulator FREE.
Click here now to try it.

New Here? Please read this important info!!!
August 16th, 2018, 06:04 PM  #1 
Member
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 582

Convert Decimal to Fractions
Anyone have a good way to convert a decimal number to fractions. Need to convert decimal inches to fractions before sending to HMI

August 16th, 2018, 06:20 PM  #2 
Lifetime Supporting Member + Moderator

Div by 12 for inches
Mod by 12 then div remainder by desired denominator ? 
August 16th, 2018, 08:45 PM  #3  
Member
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 582

Quote:
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. 

August 17th, 2018, 10:16 AM  #4 
Member
Join Date: Sep 2009
Location: Pleasant Dale, Nebraska
Posts: 135

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. 
August 17th, 2018, 10:56 AM  #5 
Member
Join Date: Jul 2005
Location: Heath, TX
Posts: 2,026

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. 
August 17th, 2018, 12:43 PM  #6 
Lifetime Supporting Member + Moderator

While (numerator % 2)=0
numerator = numerator / 2 denominator = denominator / 2 End while Or just use metric
__________________
Controlling outputs is the PLC's way of getting its inputs to change. www.thePLCguy.com 
August 18th, 2018, 09:47 PM  #7 
Member
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 582


August 19th, 2018, 04:18 AM  #8 
Member
Join Date: Jan 2013
Location: UK
Posts: 809

Times by 25.4 and put mm on the end.

August 19th, 2018, 05:46 AM  #9 
Member
Join Date: Feb 2012
Location: Bay of Plenty
Posts: 967


August 19th, 2018, 07:15 AM  #10 
Member

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, 
August 19th, 2018, 02:06 PM  #11  
Member

Quote:
I have a cool program that will calculate gear ratios given any number and desired precision. The denominator could be anything.
__________________
"Living is easy with eyes closed, misunderstanding all you see...." Strawberry Fields Forever, John Lennon 

August 19th, 2018, 06:20 PM  #12 
Member
Join Date: Apr 2009
Location: Kalieefornia
Posts: 659

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
__________________
I have to use Rockwell Software! Last edited by JaxGTO; August 19th, 2018 at 06:22 PM. 
August 20th, 2018, 08:13 AM  #13 
Member
Join Date: Jan 2011
Location: NC
Posts: 1,005

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 Fraction37.PNG Fraction8.PNG 
August 20th, 2018, 08:59 AM  #14 
Member
Join Date: Jan 2011
Location: NC
Posts: 1,005

Just noticed a typo in Rung 0. The first instruction should be GEQ not GRT.

August 20th, 2018, 09:19 AM  #15 
Member

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
__________________
"The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge." Stephen Hawking Last edited by Mispeld; August 20th, 2018 at 09:21 AM. Reason: inserted attached images 
Bookmarks 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)  
Thread Tools  
Display Modes  


Similar Topics  
Thread  Thread Starter  Forum  Replies  Last Post 
How To convert decimal to hexadecimal  squevillon  LIVE PLC Questions And Answers  21  December 11th, 2018 08:55 AM 
Convert Decimal Checklist to Binary  JJH  LIVE PLC Questions And Answers  7  November 20th, 2017 06:32 PM 
Convert number to Number of decimal places  TheWaterboy  LIVE PLC Questions And Answers  18  July 30th, 2015 07:04 PM 
Motor control program(RSlogix 5000)  kamalaitounejjar  LIVE PLC Questions And Answers  7  May 1st, 2006 08:29 AM 
Experts! Help Me Convert decimal to ASCII string!!  wlemst88  LIVE PLC Questions And Answers  16  November 6th, 2004 06:12 PM 