NumberFormat devilry

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

NumberFormat devilry

Post by dunbarx » Wed Apr 19, 2017 8:42 pm

In a small stack I have two buttons and a field. In one button script:

Code: Select all

on mouseUp
    put "" into fld 1
   put 6 into temp
   set the numberFormat to "#.000"
   put temp + 0 into fld 1
end mouseUp
Not rocket science, I get "6.000". Now in the other button script:

Code: Select all

on mouseUp
   put "" into fld 1
   put padThree(6) into fld 1
end mouseUp

function padThree var
   set the numberFormat to "#.000"
  add 0 to var
  --OPPTIONS           
  return var                   -- puts 6
  return var & "K"          --puts 6.000K
  return var & quote          --puts 6.000"
  return char 1 to 5 of var & "K" -- puts 6.000
end padThree
Any char at all in place of that "K" will preserve the three digit decimal. The truncated string does as well. The question is this: what is it about calling a function as opposed to straight inLine code? What goes awry with what I see as a very similar process? I struggled with this for a while before I simply included the quote workaround and moved on.

Craig

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

Re: NumberFormat devilry

Post by Klaus » Wed Apr 19, 2017 9:01 pm

Hi Craig,

there was a long thread in the use-list about numberformat pros and cons some time ago.

Don't remember exactly the tech stuff but the conclusion was that using the FORMAT()
function is the better choice for formatting numbers! :D

So use this instead:

Code: Select all

function padThree tVar
   return format("%0.3f",tVar)
end padThree
I change var to tVar since var got colorized in the script editor, no idea why, but why tempt fate? 8)


Best

Klaus

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

Re: NumberFormat devilry

Post by dunbarx » Wed Apr 19, 2017 9:14 pm

Klaus.

Thanks. I must remember not to be so set in my ways.

Craig

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

Re: NumberFormat devilry

Post by Klaus » Wed Apr 19, 2017 9:16 pm

Hi Craig,

a very good idea, will make you life much easier!
At least it made mine. :D


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: NumberFormat devilry

Post by jacque » Thu Apr 20, 2017 3:54 pm

The mailing list thread included some enlightening stuff from Mark Waddingham, it's worth reading. Basically, numberFormat only works on strings. Adding an alpha character creates a string, as does putting the text into a field. Math operations do something similar but (apparently) only within the handler; when the function returns the number it does so as a number, not a string. Or else the receiving handler interprets it as a number.

You could probably add the zero in the calling handler instead of the function and make it work.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Talking LiveCode”