You are not registered yet. Please click here to register!


 
 
plc storereviewsdownloads
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.

---------->>>>>Get FREE PLC Programming Tips

New Here? Please read this important info!!!


Go Back   PLCS.net - Interactive Q & A > PLCS.net - Interactive Q & A > LIVE PLC Questions And Answers

PLC training tools sale

Reply
 
Thread Tools Display Modes
Old August 16th, 2018, 06:04 PM   #1
Tim Ganz
Member
United States

Tim Ganz is offline
 
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 577
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
  Reply With Quote
Old August 16th, 2018, 06:20 PM   #2
TheWaterboy
Lifetime Supporting Member + Moderator
United States

TheWaterboy is offline
 
TheWaterboy's Avatar
 
Join Date: May 2006
Location: State of Denial
Posts: 812
Div by 12 for inches
Mod by 12 then div remainder by desired denominator ?
  Reply With Quote
Old August 16th, 2018, 08:45 PM   #3
Tim Ganz
Member
United States

Tim Ganz is offline
 
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 577
Quote:
Originally Posted by TheWaterboy View Post
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.
  Reply With Quote
Old August 17th, 2018, 10:16 AM   #4
jaden
Member
United States

jaden is offline
 
Join Date: Sep 2009
Location: Pleasant Dale, Nebraska
Posts: 116
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.
  Reply With Quote
Old August 17th, 2018, 10:56 AM   #5
keithkyll
Member
United States

keithkyll is offline
 
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.
  Reply With Quote
Old August 17th, 2018, 12:43 PM   #6
bernie_carlton
Lifetime Supporting Member + Moderator
United States

bernie_carlton is offline
 
bernie_carlton's Avatar
 
Join Date: Apr 2002
Location: Yakima, Washington
Posts: 6,248
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
  Reply With Quote
Old August 18th, 2018, 09:47 PM   #7
Tim Ganz
Member
United States

Tim Ganz is offline
 
Join Date: Dec 2010
Location: Dallas, Texas
Posts: 577
Quote:
Originally Posted by bernie_carlton View Post
While (numerator % 2)=0
numerator = numerator / 2
denominator = denominator / 2
End while


Or just use metric

Not sure how i would implement this in AB land?
  Reply With Quote
Old August 19th, 2018, 04:18 AM   #8
AustralIan
Member
United Kingdom

AustralIan is offline
 
Join Date: Jan 2013
Location: UK
Posts: 780
Times by 25.4 and put mm on the end.
  Reply With Quote
Old August 19th, 2018, 05:46 AM   #9
Saffa
Member
New Zealand

Saffa is offline
 
Join Date: Feb 2012
Location: Bay of Plenty
Posts: 914
Quote:
Originally Posted by AustralIan View Post
Times by 25.4 and put mm on the end.
Cease this heresy immediately!
  Reply With Quote
Old August 19th, 2018, 07:15 AM   #10
Aardwizz
Member
United Kingdom

Aardwizz is offline
 
Aardwizz's Avatar
 
Join Date: Dec 2017
Location: Swindon
Posts: 198
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,
  Reply With Quote
Old August 19th, 2018, 02:06 PM   #11
Peter Nachtwey
Member
United States

Peter Nachtwey is offline
 
Peter Nachtwey's Avatar
 
Join Date: Apr 2002
Location: Vancouver, WA, US
Posts: 6,735
Quote:
Originally Posted by Tim Ganz View Post
Anyone have a good way to convert a decimal number to fractions. Need to convert decimal inches to fractions before sending to HMI
Yes, but what do you want for the denominator?
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
  Reply With Quote
Old August 19th, 2018, 06:20 PM   #12
JaxGTO
Member
United States

JaxGTO is offline
 
Join Date: Apr 2009
Location: Kalieefornia
Posts: 635
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.
  Reply With Quote
Old August 20th, 2018, 08:13 AM   #13
cwal61
Member
United States

cwal61 is offline
 
Join Date: Jan 2011
Location: NC
Posts: 980
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
  Reply With Quote
Old August 20th, 2018, 08:59 AM   #14
cwal61
Member
United States

cwal61 is offline
 
Join Date: Jan 2011
Location: NC
Posts: 980
Just noticed a typo in Rung 0. The first instruction should be GEQ not GRT.
  Reply With Quote
Old August 20th, 2018, 09:19 AM   #15
Mispeld
Member
United States

Mispeld is offline
 
Mispeld's Avatar
 
Join Date: Feb 2017
Location: VA
Posts: 374
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
  Reply With Quote
Reply
Jump to Live PLC Question and Answer Forum

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Convert Decimal Checklist to Binary JJH LIVE PLC Questions And Answers 7 November 20th, 2017 06:32 PM
How To convert decimal to hexadecimal squevillon LIVE PLC Questions And Answers 18 September 10th, 2015 10:05 AM
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


All times are GMT -5. The time now is 08:30 PM.


.