Jump to content

Imperial to Metric conversion


dansxmods
 Share

Recommended Posts

With the recent birth of my second daughter I wanted to convert her weight in kilos to pounds and ounces. I did a quick search on the net and found only one coversion page that would convert to pounds and ounces and thought to myself I could do a quick script for that purpose. Here is what I wrote.

#include <GUIConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Imp2Met It", 400, 150, -1, -1)
$kgsInput = GUICtrlCreateInput("4.85", 120, 20, 97, 21)
$lbsLabel = GUICtrlCreateLabel("Pounds", 70, 55, 40, 21)
$lbsInput = GUICtrlCreateInput("10", 110, 50, 40, 21)
GUICtrlSetState($lbsLabel, $gui_hide)
GUICtrlSetState($lbsInput, $gui_hide)
$ozLabel = GUICtrlCreateLabel("Ounces", 155, 55, 40, 21)
$ozInput = GUICtrlCreateInput("11", 194, 50, 40, 21)
GUICtrlSetState($ozInput, $gui_hide)
GUICtrlSetState($ozLabel, $gui_hide)
$lblGrams = GUICtrlCreateLabel("Kilograms", 70, 20, 45, 17)
$lblPoundsOunces = GUICtrlCreateLabel("", 88, 90, 250, 50)
$Button1 = GUICtrlCreateButton("Convert", 248, 20, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Change", 248, 50, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

HotKeySet("{enter}", "convert")
HotKeySet("{Esc}", "Kill")
HotKeySet("^s", "show_hide")

$show_hide = 0

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            convert()
        Case $Button2
            show_hide()
    EndSwitch
WEnd

Func calculate2lbs($input)
    Dim $return[2]
    $pound = StringFormat("%.2f",$input * 2.2)
    $ounce = Round(StringRight($pound, 2) * .16)
    $pound = StringFormat("%d", $pound)
    $return[0] = $pound
    $return[1] = $ounce
    Return $return
EndFunc

Func Kill()
    Exit
EndFunc

Func convert()
    If $show_hide = 0 Then
        $kilo = GUICtrlRead($kgsInput)
        $kilograms = calculate2lbs($kilo)
        GUICtrlSetData($lblPoundsOunces, $kilograms[0] &  " Pounds and " & $kilograms[1] & " Ounces")
        GUICtrlSetFont($lblPoundsOunces, 16, "", "")
        GUICtrlSetState($kgsInput, $GUI_FOCUS)
    ElseIf $show_hide = 1 Then
        $pound = GUICtrlRead($lbsInput)
        $ounces = GUICtrlRead($ozInput)
        $kilograms = calculate2kgs($pound, $ounces)
        GUICtrlSetData($lblPoundsOunces, $kilograms & " Kilograms")
        GUICtrlSetFont($lblPoundsOunces, 16, "", "")
        GUICtrlSetState($kgsInput, $GUI_FOCUS)
    EndIf
    
EndFunc

Func calculate2kgs($input1, $input2)
    Dim $return[2]
    $ounce = StringFormat("%d", $input2 / .16)
    $decimal_pound = $input1 & "." & $ounce
    $kilograms = StringFormat("%.2f",$decimal_pound / 2.2)
    Return $kilograms
EndFunc

Func show_hide()
    If $show_hide = 0 Then
        GUICtrlSetState($lbsInput, $gui_show)
        GUICtrlSetState($ozInput, $gui_show)
        GUICtrlSetState($lbsLabel, $gui_show)
        GUICtrlSetState($ozLabel, $gui_show)
        GUICtrlSetState($kgsInput, $gui_hide)
        GUICtrlSetState($lblGrams, $gui_hide)
        $show_hide = 1
    ElseIf $show_hide = 1 Then
        GUICtrlSetState($lbsInput, $gui_hide)
        GUICtrlSetState($ozInput, $gui_hide)
        GUICtrlSetState($lbsLabel, $gui_hide)
        GUICtrlSetState($ozLabel, $gui_hide)
        GUICtrlSetState($kgsInput, $gui_show)
        GUICtrlSetState($lblGrams, $gui_show)
        $show_hide = 0
    EndIf
    
EndFunc

You can convert from imperial to metric and vice versa. I am considering putting more conversions in with it when I start getting more sleep.

Daniel.

Link to comment
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
 Share

  • Recently Browsing   0 members

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