HP Forums

Full Version: Using the CONVERT() function in program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
[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;
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;
Another way to approach this is to multiply the value by a 1 of the unit you want.

var := var * 1_km
Largely more elegant than my string usage.
I don’t know why, I thought this multiplication was not authorized in a program!?
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]
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.
Reference URL's