Problem, get a result of a calculation to two decimal places.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
CAsba
Posts: 363
Joined: Fri Sep 30, 2022 12:11 pm

Problem, get a result of a calculation to two decimal places.

Post by CAsba » Wed Mar 22, 2023 11:28 am

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 ?

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Problem, get a result of a calculation to two decimal places.

Post by Klaus » Wed Mar 22, 2023 11:34 am

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)

CAsba
Posts: 363
Joined: Fri Sep 30, 2022 12:11 pm

Re: Problem, get a result of a calculation to two decimal places.

Post by CAsba » Wed Mar 22, 2023 12:25 pm

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.

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Problem, get a result of a calculation to two decimal places.

Post by Klaus » Wed Mar 22, 2023 12:39 pm

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

CAsba
Posts: 363
Joined: Fri Sep 30, 2022 12:11 pm

Re: Problem, get a result of a calculation to two decimal places.

Post by CAsba » Wed Mar 22, 2023 1:11 pm

That's great, Klaus. Many, many thanks.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem, get a result of a calculation to two decimal places.

Post by dunbarx » Wed Mar 22, 2023 1:59 pm

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

CAsba
Posts: 363
Joined: Fri Sep 30, 2022 12:11 pm

Re: Problem, get a result of a calculation to two decimal places.

Post by CAsba » Wed Mar 22, 2023 2:38 pm

Hi Craig,
Gottit ! Many thanks.

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Problem, get a result of a calculation to two decimal places.

Post by stam » Thu Mar 23, 2023 1:06 am

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

Code: Select all

round(<number>, <precision>)
is it not?

S.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem, get a result of a calculation to two decimal places.

Post by dunbarx » Thu Mar 23, 2023 1:57 am

Stam.

We agree, you just don't know it yet. :wink:
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

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Problem, get a result of a calculation to two decimal places.

Post by stam » Fri Mar 24, 2023 8:43 pm

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

Code: Select all

divide field 1 by field 2
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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem, get a result of a calculation to two decimal places.

Post by dunbarx » Fri Mar 24, 2023 9:27 pm

Stam.

I hate LC.

I KNOW I tried this, and it did not work. But now it does. :|

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Problem, get a result of a calculation to two decimal places.

Post by dunbarx » Fri Mar 24, 2023 9:32 pm

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”