Page 1 of 1

Format 1234 into 1,234.00

Posted: Tue Aug 09, 2011 5:16 am
by BarrySumpter
I've had a look at the Format function in the dictionary
but very hard to follow as no practical examples and their outcomes.
Looks almost like assembler.


I'm used to something like this:
xString = format ("z,zz0.00", 1234)
resulting in: 1,234.00

Anyone have a routine or two like this to help get me started?

Still looking ...

Also, if there is a function in the DataGrid that will assist with this formatting
that would be super cool.

Re: Format 1234 into 1,234.00

Posted: Tue Aug 09, 2011 8:25 am
by bn
Hi Barry,

have a look at this:

http://runtime-revolution.278305.n4.nab ... 27469.html

the topic just came up on the use-Livecode-list.

Kind regards

Bernd

Re: Format 1234 into 1,234.00

Posted: Tue Aug 09, 2011 9:05 am
by BarrySumpter
How fortuatous!

I started with the Scriptors Scrapbook and have been moding ever since the original post
Don't use this yet as I'm starting to work on the pDecPlaces and cleaning up to look and sound better:

Code: Select all

function fmtNum pNum, pCurrency, pSeparator, pDecimal, pDecPlaces
   
   -- Syntax: fmtNum pNum [,pCurrency[,pSeparator[,pDecimal]]]
   
   if pNum is not a number then 
      return "ERR: Not a number"   -- return exist this routine
   end if
   
   if char 1 of pNum = "-" then
      put "-" into prefix
      put char 2 to -1 of pNum into pNum
   end if
   
   
   -- is Decimal = . then use decimal
   if pDecimal="" then 
      -- put "" into pDecimal
      set the numberFormat to "0"
   else
      -- otherwise don't use decimal
      -- put "." into pDecimal
      set the numberFormat to "0.00"
   end if
   
   -- force format onto number parameter
   add 0 to pNum
   
   
   
   if pDecPlaces is not empty then
      if pDecPlaces is a number then 
         put round(pnum,pDecPlaces) into pnum
      end if
   end if
   
   
   
   set the itemDelimiter to "."
   
   -- Integer.Decimal
   
   put item 1 of pNum into pInteger
   put item 2 of pNum into holdDecimal
   
   -- if the seperator not empty then use it otherwise don't
   if pSeparator is not empty then 
      repeat with x = length(pInteger)-3 to 3 step -3
         
         put pSeparator before char x+1 of pInteger
         
      end repeat
   end if
   
   
   
   return (pCurrency && prefix & pInteger & pDecimal & holdDecimal)
   
end fmtNum
You can see where I'm going here.
pCurrency can be something like "AU $" or "$AU "or just "$" , or empty etc.
pSeperator(,) and pDecimal(.) can be any character. If it ain't there then don't use it etc.


This code looks terrible with no LiveCode text color on the forum.
Is there a way I can get the code to display with the same colors as LiveCode text editor (no mods).

Re: Format 1234 into 1,234.00

Posted: Tue Aug 09, 2011 1:26 pm
by Klaus
Barry,
BarrySumpter wrote:Is there a way I can get the code to display with the same colors as LiveCode text editor (no mods).
No.

But you can also use -> format(xxxxx) in LiveCode! Check the dictionary!
Maybe that will do what you need?


Best

Klaus

Re: Format 1234 into 1,234.00

Posted: Sun Aug 21, 2011 1:53 am
by BarrySumpter
Just stumbled on this LiveCode article:

39268-How-Do-I-Create-a-Formatting-Pattern-for-a-Column- (using Data Grid Helper)
http://lessons.runrev.com/s/lessons/m/4 ... -a-Column-

Seems I forgot that the Data Grid Helper was included in my OmegaBundle purchase.
Still giggling like a little girl when I realize just how great the OmegaBundle is.

Re: Format 1234 into 1,234.00

Posted: Tue Jan 21, 2014 12:55 pm
by FLCo
Use the FieldFormatter extension to LiveCode properties. It is designed to do this automatically, along with many other display formatting options...

Link: http://www.FlexibleLearning.com/fieldformatter