veda2010

Looking for MM to Inch (fraction) conveter.

7 posts in this topic

Hello Everyone. 

This is Veda, First of all, i'm not familiar with AutoIt tool. I heard about this in internet about Automation of few tasks, Apps, etc.

I am looking for a some tool to show the MM to Inch conversion in Fractions. Can anyone help me with this? It would be better if there is GUI converter for this.

Example:

20.63 MM = 13/16"

 

Thanks

Veda

Share this post


Link to post
Share on other sites



veda2010,

Welcome to the AutoIt forums. :)

Here is a simple example which should get you started: ;)

#include <GUIConstantsEx.au3>

; Create a GUI
$hGUI = GUICreate("Test", 500, 500)

; Create a couple of inputs
GUICtrlCreateLabel("Inches", 10, 20, 200, 30)
$cInches = GUICtrlCreateInput("", 10, 50, 200, 40)
GUICtrlSetFont($cInches, 24)

GUICtrlCreateLabel("Millimetres", 10, 120, 200, 30)
$cMilli = GUICtrlCreateInput("", 10, 150, 200, 40)
GUICtrlSetFont($cMilli, 24)

; And a couple of buttons
$cConvert = GUICtrlCreateButton("Convert", 10, 300, 80, 30)
$cClear = GUICtrlCreateButton("Clear", 200, 300, 80, 30)

GUISetState()

While 1

    ; Look for an event to occur
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE ; Close [X]
            Exit
        Case $cClear ; Clear button
            ; So empty the inputs
            GUICtrlSetData($cInches, "")
            GUICtrlSetData($cMilli, "")
        Case $cConvert ; Convert button
            ; If there is something in the "inches" input
            If GUICtrlRead($cInches) <> "" Then
                ; Convert it to mm and display the result
                $nMM = Number(GUICtrlRead($cInches)) * 254
                GUICtrlSetData($cMilli, $nMM)
            Else
                ; We need to convert mm to inches
                $nInches = Round(GUICtrlRead($cMilli) / 254, 5)
                GUICtrlSetData($cInches, $nInches)
            EndIf
    EndSwitch

WEnd
As you say you are new to AutoIt (and no problem there because we all were at some time) reading the Help file (at least the first few sections - Using AutoIt, Tutorials and the first couple of References) will help you enormously. You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page). :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I modified M23's example to display the result as fraction.

#include <GUIConstantsEx.au3>

; Create a GUI
$hGUI = GUICreate("Test", 500, 500)

; Create a couple of inputs
GUICtrlCreateLabel("Inches", 10, 20, 200, 30)
$cInches = GUICtrlCreateInput("", 10, 50, 400, 40)

GUICtrlSetFont($cInches, 24)

GUICtrlCreateLabel("Millimetres", 10, 120, 200, 30)
$cMilli = GUICtrlCreateInput("", 10, 150, 400, 40)
GUICtrlSetFont($cMilli, 24)

; And a couple of buttons
$cConvert = GUICtrlCreateButton("Convert", 10, 300, 80, 30)
$cClear = GUICtrlCreateButton("Clear", 200, 300, 80, 30)

GUISetState()

While 1

    ; Look for an event to occur
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE ; Close [X]
            Exit
        Case $cClear ; Clear button
            ; So empty the inputs
            GUICtrlSetData($cInches, "")
            GUICtrlSetData($cMilli, "")
        Case $cConvert ; Convert button
            ; If there is something in the "inches" input
            If GUICtrlRead($cInches) <> "" Then
                ; Convert it to mm and display the result
                $nMM = Number(GUICtrlRead($cInches)) * 254
                GUICtrlSetData($cMilli, Float2Frac($nMM))
                GUICtrlSetTip($cMilli, $nMM)
            Else
                ; We need to convert mm to inches
                $nInches = Round(GUICtrlRead($cMilli) / 254, 5)
                GUICtrlSetData($cInches, Float2Frac($nInches))
                GUICtrlSetTip($cInches, $nInches)
            EndIf
    EndSwitch

WEnd


Func Float2Frac($fFloat, $iAccuracy = 5) ;coded by UEZ
    If Not IsNumber($fFloat) Then Return SetError(1, 0, 0)
    Local $iDec = StringLen(StringRegExpReplace($fFloat, "\d+\.(\d*)", "\1"))
    $iAccuracy = $iAccuracy > 10 ? 10 : $iAccuracy
    $iDec = $iDec > $iAccuracy ? $iAccuracy : $iDec
    Local $iZaehler = Round($fFloat * 10 ^ $iDec, 0)
    Local $iNenner = 10 ^ $iDec
    Local $iGGT = Number(ggT(Int($iZaehler), Int($iNenner)))
    ConsoleWrite("Float: " & $fFloat & @CRLF)
    ConsoleWrite("Dec: " & $iDec & @CRLF)
    ConsoleWrite("Zaehler: " & $iZaehler & @CRLF)
    ConsoleWrite("Nenner: " & $iNenner & @CRLF)
    ConsoleWrite("$iGGT: " & $iGGT & @CRLF)
    ConsoleWrite($fFloat & " -> " & $iZaehler / $iGGT & " / " & $iNenner / $iGGT & @CRLF & @CRLF)
    If $iGGT < 0 Then
        $iZaehler /= $iGGT * -1
        $iNenner /= $iGGT * -1
    Else
        $iZaehler /= $iGGT
        $iNenner /= $iGGT
    EndIf
    Return $iZaehler & " / " & $iNenner
EndFunc   ;==>Float2Frac


Func ggT($a, $b) ;coded by UEZ 2012
    If Not IsInt($a) Then Return SetError(1, 0, 0)
    If Not IsInt($b) Then Return SetError(2, 0, 0)
    If $a = $b Then Return Abs($a)
    If Not $a And $b Then Return Abs($a)
    If $a And Not $b Then Return Abs($b)
    If ($a And $b = 1) Or ($a = 1 And $b) Then Return 1
    Local $iMod
    Do
        $iMod = Mod($a, $b)
        If Not $iMod Then ExitLoop
        $a = $b
        $b = $iMod
    Until False
    Return $b
EndFunc

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Thanks for the quick response M23 & UEZ.

I was trying to run the script and it says some error

Error Message 

 $iAccuracy= $iAccuracy>10 ? 10 : $iAccuracy

 $iAccuracy =  $iAccuracy > 10 ^ ERROR

Share this post


Link to post
Share on other sites

veda2010,

Those ternary operators were introduced in v3.3.10.0 - what version are you running? :huh:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I'm using  Version 3.3.8.1

let me try with new version.

 

Thanks

Veda

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Thanks again UEZ. & M23.

I tried with latest version (v3.3.12) and the code working fine. 

 

Thanks

Veda

Edited by veda2010

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now