Inserting comma in numbers

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
urbaud
Posts: 120
Joined: Tue Feb 24, 2009 12:10 am

Inserting comma in numbers

Post by urbaud » Fri Aug 21, 2009 2:39 am

How would I insert a comma in the following number: 27155.34? I would like to know how to format the number so it will look like this: 27,155.34. I can't find any reference to it in the Dictionary or in Dan Shafer's book. Can anyone help?
urbaud

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Fri Aug 21, 2009 8:45 am

Urbaud,

You might use this function.

Code: Select all

constant dot = "."
function formatNumber theNumber
     put fld 1 into theNumber
     if theNumber contains dot then
          set the itemDelimiter to dot
          put last item of theNumber into myDecimals
          delete last item of theNumber
     else
          put "00" into myDecimals
     end if
     repeat with x = (number of chars of theNumber- 2) down to 4 step -3
          put comma before char x of theNumber
     end repeat
     return theNumber & dot & myDecimals
end formatNumber
Best regards,

Mark
Last edited by Mark on Fri Aug 21, 2009 10:46 am, edited 1 time in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Aug 21, 2009 9:56 am

Hi Urbaud,
Just remember that when you retrieve a number displayed with the comma inserted, then to RunRev it is actually handled as a string.
There is some more info about number formats and how to handle them in the forum, this thread for instance http://forums.runrev.com/phpBB2/viewtop ... 2696#12696 refers to setProp/getProp solutions to handle the display and retrieval options - which is, of course, only one of the ways RunRev lets you skin the cat.

Post Reply