Unit Converter
Posted: Wed May 29, 2024 5:44 am
I'm looking for a sample code for a unit converter.
As in JavaScript, no button may be used.
Blitz
As in JavaScript, no button may be used.
Blitz
What do you mean with Sample Code for Unit Converter? Like "display units" or like "Celsius to Fahrenheit"?Blitz wrote: Wed May 29, 2024 5:44 am I'm looking for a sample code for a unit converter.
As in JavaScript, no button may be used.
Blitz
Code: Select all
<p align="center"><font size="5" face="Arial"><b>Converter: Fahrenheit to Celsius and Celsius to Fahrenheit</b></font></p>
<table border="0" align="center" cellpadding="0" cellspacing="0" style="background:#FFFFFF">
<tr>
<td><font size="4" face="Arial">To use the calculator, simply enter a value.<br>
The calculator works in both directions of the <font face="Arial" size="5"><b>↔</b></font> sign.</font></td>
</tr>
</table>
<br>
<form method="post" action="">
<table border="1" align="center" cellpadding="0" cellspacing="0" style="background:#EFEFEF">
<tr>
<td colspan="4" align="center"><font size="1" face="Arial"> </font></td>
</tr>
<tr>
<td width="210"><font face="Arial"><b>Temperature in Fahrenheit:</b><br>
<input name="F" value="32" size="12" onKeyUp="C.value=(100/(212-32)*(this.value-32)).toFixed(2);" style="background:#FFFFFF">
 °F</font></td>
<td width="39"><font face="Arial" size="5"><b> ↔ </b></font></td>
<td width="210"><font face="Arial"><b>Temperature in Celsius:</b><br>
<input name="C" value="0" size="12" onKeyUp="F.value=((212-32)/100*this.value+32).toFixed(2);" style="background:#FFFFFF">
 °C</font></td>
</tr>
<tr style="background:#FFFFFF">
<td><font face="Arial"><b> °F = °C × 1.8 + 32</b></font></td>
<td><font face="Arial"> </font></td>
<td><font face="Arial"><b> °C = (°F − 32) <i>/</i> 1.8</b></font></td>
</tr>
<tr>
<td colspan="4" align="center"><font size="1" face="Arial"> </font></td>
</tr>
</table>
</form>
Code: Select all
Define EventID,MenuID,GadgetID,WindowID
Define.d cel,fah
Enumeration 1
#Window_Main
EndEnumeration
Enumeration 1
#Gadget_Main_Text2
#Gadget_Main_Text3
#Gadget_Main_cel
#Gadget_Main_fah
EndEnumeration
Procedure.i Window_Main()
If OpenWindow(#Window_Main,0,0,240,132,"Temperature Converter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
TextGadget(#Gadget_Main_Text2,15,30,60,20,"Celsius")
TextGadget(#Gadget_Main_Text3,135,30,80,20,"Fahrenheit")
StringGadget(#Gadget_Main_cel,15,50,80,20,"")
StringGadget(#Gadget_Main_fah,135,50,80,20,"")
HideWindow(#Window_Main,#False)
ProcedureReturn WindowID(#Window_Main)
EndIf
EndProcedure
Procedure GadgetEvent()
GadgetID=EventGadget()
Select GadgetID
Case #Gadget_Main_cel
If EventType()=#PB_EventType_Change
cel=ValD(GetGadgetText(#Gadget_Main_cel))
SetGadgetText(#Gadget_Main_fah,StrD((cel*1.8)+32,2))
EndIf
Case #Gadget_Main_fah
If EventType()=#PB_EventType_Change
fah=ValD(GetGadgetText(#Gadget_Main_fah))
SetGadgetText(#Gadget_Main_cel,StrD((fah-32)/1.8,2))
EndIf
EndSelect
EndProcedure
If Window_Main()
BindEvent(#PB_Event_Gadget,@GadgetEvent())
EndIf
Code: Select all
Procedure Convert(Value, FromUnit.s, ToUnit.s)
! return convert(v_value, v_fromunit).to(v_tounit);
EndProcedure
Procedure Main()
Debug("360 seconds are " + Convert(360, "seconds", "minutes") + " minutes")
Debug("451 °F are " + StrF(Convert(451, "fahrenheit", "celsius"), 2) + " °C")
EndProcedure
! require(["https://cdn.jsdelivr.net/npm/convert@5/dist/index.js"], function(v) {
! window.convert = v.convert;
Main()
! });