Unit Converter

Using Javascript from SpiderBasic
Blitz
Posts: 8
Joined: Sat Aug 25, 2018 8:29 am
Contact:

Unit Converter

Post by Blitz »

I'm looking for a sample code for a unit converter.

As in JavaScript, no button may be used.

Blitz
User avatar
bembulak
Posts: 95
Joined: Wed Feb 26, 2014 9:53 am

Re: Unit Converter

Post by bembulak »

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 mean with Sample Code for Unit Converter? Like "display units" or like "Celsius to Fahrenheit"?
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
Blitz
Posts: 8
Joined: Sat Aug 25, 2018 8:29 am
Contact:

Re: Unit Converter

Post by Blitz »

Fred
Site Admin
Posts: 1820
Joined: Mon Feb 24, 2014 10:51 am

Re: Unit Converter

Post by Fred »

You should try to post a small source code when asking for help so we can iterate on it.
Blitz
Posts: 8
Joined: Sat Aug 25, 2018 8:29 am
Contact:

Re: Unit Converter

Post by Blitz »

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.

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>&#8596;</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">&#160;</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">
&#160;&deg;F</font></td>
<td width="39"><font face="Arial" size="5"><b>&#160;&#8596;&#160;</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">
&#160;&deg;C</font></td>
</tr>
<tr style="background:#FFFFFF">
<td><font face="Arial"><b>&#160;&deg;F = &deg;C &times; 1.8 + 32</b></font></td>
<td><font face="Arial">&#160;</font></td>
<td><font face="Arial"><b>&#160;&deg;C = (&deg;F &minus; 32) <i>/</i> 1.8</b></font></td>
</tr>
<tr>
<td colspan="4" align="center"><font size="1" face="Arial">&#160;</font></td>
</tr>
</table>
</form>
User avatar
Paul
Posts: 210
Joined: Wed Feb 26, 2014 6:46 pm
Location: Canada
Contact:

Re: Unit Converter

Post by Paul »

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
User avatar
Peter
Posts: 1197
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Unit Converter

Post by Peter »

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()
! });
Blitz
Posts: 8
Joined: Sat Aug 25, 2018 8:29 am
Contact:

Re: Unit Converter

Post by Blitz »

Thanks for the quick help, especially to @Peter and @Paul
Post Reply