HP Forums
Using the CONVERT() function in program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Using the CONVERT() function in program (/thread-13905.html)



Using the CONVERT() function in program - gregreenwood - 10-29-2019 11:28 PM

I would like to add units to a value input from the user. Can CONVERT() do that in a program? I have tried to do:
var:= convert(var, 1_km);
but that throws a syntax error.

Or possible there is a way to affix units to a number being input via the input() function?


RE: Using the CONVERT() function in program - pinkman - 10-30-2019 05:23 AM

[edited since precisions of tyann and John]


Hi,
You may use /*intermediate strings*/ multiplication to add units :

Code:

EXPORT MYPRG()
BEGIN
LOCAL Distance;
INPUT(Distance);
//EXPR(Distance+"_m")▶Distance;
Distance * 1_m ▶Distance;
MSGBOX(Distance);
CONVERT(Distance, 1_ft)▶Distance;
MSGBOX(Distance);
END;



RE: Using the CONVERT() function in program - Tyann - 10-30-2019 05:53 AM

Bonjour

Vous également écrire : var:=var*1_unité
par exemple :

Hello

You also write: var:=var*1_unit
for example:

Code:

var:=var*1_km;



RE: Using the CONVERT() function in program - john gustaf stebbins - 10-30-2019 05:54 AM

Another way to approach this is to multiply the value by a 1 of the unit you want.

var := var * 1_km


RE: Using the CONVERT() function in program - pinkman - 10-30-2019 06:56 AM

Largely more elegant than my string usage.
I don’t know why, I thought this multiplication was not authorized in a program!?


RE: Using the CONVERT() function in program - Carlos295pz - 10-30-2019 12:35 PM

INPUT can force you to type the unit on the data entered, but this can be annoying and slow
Code:
INPUT({{var,[9]}})

[Image: 48985609017_0e331e3653_o.png]

A more friendly alternative would be to indicate the unit in the label or help.
Code:
  INPUT(var,{},"Long (km):");
  var:=var*1_km

[Image: 48985395031_d5b9458292_o.png]

You can also achieve something more interactive with a selection of the unit
Code:
  LOCAL var,und={"mm","cm","m","km"},un;
  INPUT({{var,[0],{20,20,0}},{un,und,{42,15,0}}},{},"Long:");
  var:=EXPR(var+"_"+und(un))

[Image: 48985586042_b60c31d2d7_o.png]


RE: Using the CONVERT() function in program - Tyann - 10-30-2019 06:32 PM

Bonjour

Ha l'entrée de la valeur par saisie et de l'unité par liste de choix
me plait beaucoup.
Merci Carlos295pz pour cette excellente suggestion.


Hello

Ha the entry of the value per entry and the unit per choice list
I like it very much.
Thank you Carlos295pz for this excellent suggestion.