I'm looking for a sample code for a unit converter.
As in JavaScript, no button may be used.
Blitz
Unit Converter
Re: Unit Converter
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
What do you have so far, or what are you referring to, when you say '... As in JavaScript'?
Do you already have sort of an example? If yes, what does work / what does not work? If it does not work, what are the errors you get?
Please explain a little more. With this little information, it's quite difficult.
Thank you!
P.S.: I do have sort of a backflash. Maybe this could help you to extend the scope and goal of your questions. Some of us here - including me - are ol' grognards and need a specific form of how information is presented.
Kind regards,
bembulak
bembulak
Re: Unit Converter
The calculator works in both directions of the <--> sign.
https://sengpielaudio.com/calculator-speedsound.htm
https://sengpielaudio.com/calculator-forceunits.htm
https://sengpielaudio.com/calculator-speedsound.htm
https://sengpielaudio.com/calculator-forceunits.htm
Re: Unit Converter
You should try to post a small source code when asking for help so we can iterate on it.
Re: Unit Converter
I have copied the code from line 858 to line 889 from the first link. The code is in the paragraph:
Converter: Fahrenheit to Celsius and Celsius to Fahrenheit
It is not necessarily a code problem, but the question of whether SpiderBasic can automate the values in both directions.
Only 2 input fields are required for the calculation.
Converter: Fahrenheit to Celsius and Celsius to Fahrenheit
It is not necessarily a code problem, but the question of whether SpiderBasic can automate the values in both directions.
Only 2 input fields are required for the calculation.
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>
Re: Unit Converter
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
Re: Unit Converter
Here's a snippet using a convert-Library:
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()
! });
Re: Unit Converter
Thanks for the quick help, especially to @Peter and @Paul