Number Format

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
smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Number Format

Post by smith8867 » Mon Oct 17, 2016 12:32 pm

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.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Number Format

Post by AxWald » Mon Oct 17, 2016 1:59 pm

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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Number Format

Post by dunbarx » Mon Oct 17, 2016 2:39 pm

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

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Number Format

Post by smith8867 » Mon Oct 17, 2016 2:45 pm

Thanks a lot! Works a treat!

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Number Format

Post by AxWald » Mon Oct 17, 2016 7:09 pm

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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Number Format

Post by dunbarx » Mon Oct 17, 2016 7:57 pm

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

Post Reply