Formatting numbers
Jim Hurley
jhurley at infostations.com
Sun Jan 15 09:27:07 EST 2006
>
>Message: 21
>Date: Fri, 13 Jan 2006 15:40:10 -0800
>From: Marty Knapp <martyknapp at comcast.net>
>Subject: Formatting numbers
>To: Revolution User List <use-revolution at lists.runrev.com>
>Message-ID: <43C83A5A.2050809 at comcast.net>
>Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
>Does somebody have a suggestion for formatting numbers with commas for a
>printed report? It wil have columns of data, with numbers ranging from
>small to large. The larger ones are hard to read, so I'd like to insert
>commas at the appropriate places. I can think of clunky ways to do it,
>but thought I'd ask here in case someone had already brewed up something
>nice.
>
>Thanks,
>
>Marty Knapp
Marty,
Here are a couple of number formatting functions I use a lot.
The first is the standard comma formatting and the second puts the
number into scientific format.
For example, the number 12225677.98 formats to
Comma format: 12,225,677.98
Scientific format 1.22*10^7
Jim
function commaFormat tNum
put tNum mod 1 into remainder
delete char 1 of remainder
put trunc(tNum) into tNum
put the number of chars in tNum into n
repeat with i = 1 to trunc((n-1)/3)
put comma after char n - 3*i of tNum
end repeat
return tNum& remainder
end commaformat
function sciFormat tNum,sigFigures
if tNum < 0 then
put "-" into sign
else put empty into sign
put abs(tNum) into tNum
if sigFigures is empty then put 3 into sigFigures --Default
significant figures.
put 0 into count
if tNum >= 1 then
repeat until tNum < 10
divide tNum by 10
add 1 to count
end repeat
put round((10^(sigFigures-1))*tNum)/10^(sigFigures-1) into tNum
return sign & (char 1 to sigFigures + 1 of tNum) &"*10^" & count
end if
if tNum < 1 then
repeat until tNum >= 1
multiply tNum by 10
add 1 to count
end repeat
end if
return sign & (char 1 to sigFigures + 1 of tNum) & "*10^-" & count
end sciFormat
More information about the use-livecode
mailing list