# Imperial to Metric conversion

## 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
\$kilograms = calculate2lbs(\$kilo)
GUICtrlSetData(\$lblPoundsOunces, \$kilograms[0] &  " Pounds and " & \$kilograms[1] & " Ounces")
GUICtrlSetFont(\$lblPoundsOunces, 16, "", "")
GUICtrlSetState(\$kgsInput, \$GUI_FOCUS)
ElseIf \$show_hide = 1 Then
\$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.

##### Share on other sites

I like it, it's just plain simple =)

You could make a general app for converting lots of units to lots of other units.

[right]~What can I say, I'm a Simplistic person[/right]

## Create an account

Register a new account