Page 1 of 1

Number Format

Posted: Mon Oct 17, 2016 12:32 pm
by smith8867
I have this piece of code which works perfectly:

Code: Select all

   
   set numberformat to "0.00" -- Not sure if this is correct.
   put "SELECT SUM(amount) FROM payments WHERE completed=1" into dbSQL
   put revDataFromQuery(tab,return,connectionID,dbSQL) into dbData
   if item 1 of dbData = "revbderr" then
      answer error "There was a problem:" & cr & dbData
   end if
   put "£ "& dbData into fld "totalFunds"
However, when the dbData is put into the field, it comes out like this:
Image

I was wondering how I can fix this using the numberformat function.

Re: Number Format

Posted: Mon Oct 17, 2016 1:59 pm
by AxWald
Hi,
Important! Changing the numberFormat does not automatically change the format of a number that's already in a container. It affects numbers only when they are calculated and then displayed or used as strings. Otherwise, the number retains its full numeric precision.
Well, "numberFormat" is tricky - better leave it alone.
Hint:

Code: Select all

put "£ "& dbData*1 into fld "totalFunds"
should do the trick ...

Much more easy is:

Code: Select all

format("%." & MyPrecision & "f", MyNumber)
(put "£ "& format("%.2f",dbData) into fld "totalFunds")
Works like "numberformat" should, but has C like rounding (AFAIK).

or simply:

Code: Select all

round(MyNumber, MyPrecision)
(put "£ "& round(dbData,2) into fld "totalFunds")
Has nice financial rounding, but omits trailing zeroes.

If you want the financial rounding of "round" plus the guaranteed suiting zeroes of "format", use:

Code: Select all

function kround theNum, Prec  -- nice fin. rounding with exactly Prec items trailing
   if Prec is empty then put 2 into Prec
   return format("%." & Prec & "f",round(theNum, Prec))
end kround
(put "£ "& kround(dbData) into fld "totalFunds")

Have fun!

Re: Number Format

Posted: Mon Oct 17, 2016 2:39 pm
by dunbarx
The numberFormat is not as tricky as it may seem. Though it is true that it does not change an existing value already in memory, one can always:

Code: Select all

on mouseUp
   put "123.4567890" into temp
   set the numberformat to "0.00"
   answer temp
   answer temp + 0
end mouseUp
This forces the reformatting. This extra recalculation step I would not call a "trick" of any kind, since that is how that property was intended to work from the beginning.

Of course, if the numberFormat is set before anything is calculated, the normal method:

Code: Select all

on mouseup
   set the numberFormat to "0.00"
   answer 10/6
end mouseup
No issues.

Craig Newman

Re: Number Format

Posted: Mon Oct 17, 2016 2:45 pm
by smith8867
Thanks a lot! Works a treat!

Re: Number Format

Posted: Mon Oct 17, 2016 7:09 pm
by AxWald
Hehe,

with "tricky" (for numberformat()) I meant:
Ways too often I just forget that I have to do the calculation to trigger it,
get wrong values & have to debug then :/

It's just too good in helping me to trick myself ;-)

Have fun!

Re: Number Format

Posted: Mon Oct 17, 2016 7:57 pm
by dunbarx
AxWald.

I am with you, I forget that too. If I were more disciplined (Hmmmph, ah-hahahaha. Heh, heh. Ahem), I would set the property before I needed it.

Craig