Format 1234 into 1,234.00

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
BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Format 1234 into 1,234.00

Post by BarrySumpter » Tue Aug 09, 2011 5:16 am

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.
Last edited by BarrySumpter on Wed Aug 10, 2011 11:43 pm, edited 2 times in total.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4174
Joined: Sun Jan 07, 2007 9:12 pm

Re: Format 1234 into 1,234.00

Post by bn » Tue Aug 09, 2011 8:25 am

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

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Format 1234 into 1,234.00

Post by BarrySumpter » Tue Aug 09, 2011 9:05 am

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).
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Format 1234 into 1,234.00

Post by Klaus » Tue Aug 09, 2011 1:26 pm

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

BarrySumpter
Posts: 1201
Joined: Sun Apr 24, 2011 2:17 am

Re: Format 1234 into 1,234.00

Post by BarrySumpter » Sun Aug 21, 2011 1:53 am

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.
All my best,
Barry G. Sumpter

Deving on WinXP sp3-32 bit. LC 5.5 Professional Build 1477
Android/iOS/Server Add Ons. OmegaBundle 2011 value ROCKS!
2 HTC HD2 Latest DorimanX Roms
Might have to reconsider LiveCode iOS Developer Program.

FLCo
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 36
Joined: Thu Jul 27, 2006 5:40 am
Contact:

Re: Format 1234 into 1,234.00

Post by FLCo » Tue Jan 21, 2014 12:55 pm

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
The Flexible Learning Company
• ChartMaker: www.FlexibleLearning.com/chartmaker
• ControlManager: www.FlexibleLearning.com/controlmanager
• The Scripter's Scrapbook: www.FlexibleLearning.com/ssbk
• DatePicker: www.FlexibleLearning.com/datepicker

Post Reply