Here's what I am using for formatting a "number"; note that a "number" is
defined as any number of digits with a single dot as decimal separator.
We're not using a thousands separator; you'll have to add that yourself. And
of course: if you're not in Belgium, you had better change the "locale" (see
Java locales for details) to something more appropriate and adapt the mask
accordingly...

    <cffunction name="formatAsDecimalNumber"
        returntype="string"
        access="public"
        hint="Tries to format a string as a 'number', with 'dec' digits
after the decimal separator. Returns the input string if formatting fails.
Maximum number of decimals after the separator is 10">

        <cfargument name="number" required="true" type="string" />
        <cfargument name="dec" required="true" type="numeric" />

        <cfset var mask = "" />
        <cfset var temp = "" />
        <cfset var loc = GetLocale() />

        <cftry>
                <cfif ( arguments.dec EQ 0 ) >
                    <cfset mask = "_" />
                <cfelseif ( ( arguments.dec GT 0 ) AND ( arguments.dec LT 11
) ) >
                    <cfset mask = "_." & Left( "____________", arguments.dec
) />
                <cfelse>
                    <cfthrow message="Illegal argument for the
'formatAsNumber' method" />
                </cfif>
                <cfset SetLocale( "Dutch (Belgian)" ) />
                <cfset temp = Trim( REReplace( LSNumberFormat(
arguments.number, mask ), ",", "." ) ) />
                <cfset SetLocale( loc ) />
                <cfreturn temp />
            <cfcatch type="any">
                <cfreturn arguments.number />
            </cfcatch>
        </cftry>

    </cffunction>

Wouter


-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Reactor for ColdFusion Mailing List
[EMAIL PROTECTED]
Archives at: http://www.mail-archive.com/reactor%40doughughes.net/
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Reply via email to