Jump to content

Recommended Posts

Posted

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

  • Moderators
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

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

  • Moderators
Posted

veda2010,

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

Thanks again UEZ. & M23.

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

 

Thanks

Veda

Edited by veda2010

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...