Page 1 of 1
Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 11:28 am
by CAsba
Hi,
It seems to be impossible to set the output of a calculation to 2 decimal places.
I have tried both
Code: Select all
divide field "packprice" by fld "usualquantity"
put format("%.2f",fld "packprice")
put fld "packprice" into fld "cost"
and
Code: Select all
divide field "packprice" by fld "usualquantity"
set the numberformat to "0.00"
put fld "packprice" into fld "cost"
that I gleaned ftrom previous topics, but neither work.
Any ideas ?
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 11:34 am
by Klaus
I never compute FIELDS directly, so maybe this is not supported?
Put their content into variables and try again.
Code: Select all
divide field "packprice" by fld "usualquantity"
set the numberformat to "0.00"
Hint:
You need to set the numberformat BEFORE doing your calculation,
since it will affect only the result of a mathematical operation.
However this does not always work so I now always use the format thing:
format("%.2f",a_variable)
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 12:25 pm
by CAsba
Hi Klaus,
Many thanks for your quick response.
I tried
Code: Select all
ask "Enter the quantity in the pack purchased"
put it into quant
ask " Enter the cost of the pack purchased"titled (fld "fieldboxtitle" of cd "template1")
put it into cost
put format("%.2f",cost)
put cost/quant into fld "cost"
-- divide cost by quant
-- put cost into fld "cost"
#put it into fld "cost"
exit mouseup
(the commente out bits I also tried)
and the field "cost" remains a 6 decimal place figure.
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 12:39 pm
by Klaus
Hi CAsba,
you need to "format" the last result, too!
Code: Select all
...
ask "Enter the quantity in the pack purchased"
put it into quant
ask " Enter the cost of the pack purchased"titled (fld "fieldboxtitle" of cd "template1")
put it into cost
## put format("%.2f",cost)
## Or use this directly in FORMAT:
put format("%.2f",cost/quant) into fld "cost"
exit mouseup
...
Best
Klaus
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 1:11 pm
by CAsba
That's great, Klaus. Many, many thanks.
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 1:59 pm
by dunbarx
CAsba.
Formatting aside, you cannot divide a field by another field. I assume your intent was to have the value in field "packPrice", after being divided by fld "usualquantity", being replaced back into that field? Though I see your thinking, that is outside the scope of LC syntax.
I would have:
Code: Select all
set the numberformat to "0.00"
put fld "packPrice" / fld "usualquantity" into field "packPrice"
Do you see how this is more "standard"?
Craig
Re: Problem, get a result of a calculation to two decimal places.
Posted: Wed Mar 22, 2023 2:38 pm
by CAsba
Hi Craig,
Gottit ! Many thanks.
Re: Problem, get a result of a calculation to two decimal places.
Posted: Thu Mar 23, 2023 1:06 am
by stam
dunbarx wrote: ↑Wed Mar 22, 2023 1:59 pm
Formatting aside, you cannot divide a field by another field
Hi Craig,
I'm not sure that's correct - you can certainly divide (or do any other calculation) with directly wit fields. Quick test: in a new stack add 2 fields and put numbers into them. If you put "field 1 / field 2" in the message box you get the correct calculation.
Regarding formatting - wasn't clear if the OP needed exactly 2 decimals (in which case agree with the above), or
up to 2 decimals (i.e 3, 3.1, 3.11) - if up to two decimals then surely it's simpler to use the round function
is it not?
S.
Re: Problem, get a result of a calculation to two decimal places.
Posted: Thu Mar 23, 2023 1:57 am
by dunbarx
Stam.
We agree, you just don't know it yet.
put "field 1 / field 2" in the message box
I am cutting you some syntactical slack here because I know you just a little bit.
That is not what the OP originally tried to do. He tried to divide fld 1 by fld 2. Just and only exactly that. Bad syntax, though, as I said, I understood his thinking.
Craig
Re: Problem, get a result of a calculation to two decimal places.
Posted: Fri Mar 24, 2023 8:43 pm
by stam
Actually the syntax is correct - or at least works for me.
If I create a new stack and add 2 fields, put '4' into field 1 and '2' into field 2 then putting this statement into the msgbox
changes the value of field 1 accordingly. So it does work, but the differences is the value is directly put into to the container that is being divided, whereas
field 1 / field 2 can be put into a variable or wherever.
Re: Problem, get a result of a calculation to two decimal places.
Posted: Fri Mar 24, 2023 9:27 pm
by dunbarx
Stam.
I hate LC.
I KNOW I tried this, and it did not work. But now it does.
Craig
Re: Problem, get a result of a calculation to two decimal places.
Posted: Fri Mar 24, 2023 9:32 pm
by dunbarx
And I am glad it does, I guess. The syntax seems sound on the face of it. When I (really, really) tried it a couple of days ago, neither field changed, so i assumed it was just not the way LC syntax works. In other words, fields are not variables, even though they are containers.
I bet Scotland changed it yesterday.
Craig