coldfusion weird extra space
Asked Answered
M

3

7

I have a function to convert string to number

<cffunction name="convertToNumber" returntype="numeric">
    <cfargument name="separator" required="Yes" type="string" />
    <cfargument name="number" required="Yes" type="string" />

    <cfset LOCAL.arrSeparator = ["comma", "period", "lakh_crore"] />

    <cfif ARGUMENTS.separator eq "comma" or ARGUMENTS.separator eq "lakh_crore">
        <cfif ListLen(ARGUMENTS.number, ".") eq 2>
            <cfset LOCAL.integral = ListFirst(ARGUMENTS.number, ".") />
            <cfset LOCAL.fractional = ListLast(ARGUMENTS.number, ".") />
        <cfelse>
            <cfset LOCAL.integral = ARGUMENTS.number />
            <cfset LOCAL.fractional = "" />
        </cfif>

        <cfset LOCAL.integral = Replace(LOCAL.integral, ",", "", "ALL") />
        <cfset LOCAL.returnValue = LOCAL.integral />

        <cfif Len(Trim(LOCAL.fractional))>
            <cfset LOCAL.returnValue &= "." & LOCAL.fractional />
        </cfif>
    </cfif>

    <cfif ARGUMENTS.separator eq "period">
        <cfif ListLen(ARGUMENTS.number, ",") eq 2>
            <cfset LOCAL.integral = ListFirst(ARGUMENTS.number, ",") />
            <cfset LOCAL.fractional = ListLast(ARGUMENTS.number, ",") />
        <cfelse>
            <cfset LOCAL.integral = ARGUMENTS.number />
            <cfset LOCAL.fractional = "" />
        </cfif>

        <cfset LOCAL.integral = Replace(LOCAL.integral, ".", "", "ALL") />
        <cfset LOCAL.returnValue = LOCAL.integral />

        <cfif Len(Trim(LOCAL.fractional))>
            <cfset LOCAL.returnValue &= "." & LOCAL.fractional />
        </cfif>
    </cfif>

    <cfreturn LOCAL.returnValue />

</cffunction>

<cfset separatorNumber = StructNew() />
<cfset separatorNumber.comma = "1,234,567,890.123456">
<cfset separatorNumber.period = "1.234.567.890,123456">
<cfset separatorNumber.lakh_crore = "1,23,45,67,890.123456">

<cfloop collection="#separatorNumber#" item="separator">
    <p>
        #separator# :
        <input type="Text" value="#convertToNumber(separator, separatorNumber[separator])#">
        Length : #Len(convertToNumber(separator, separatorNumber[separator]))#
    </p>
</cfloop>

the length is 17, but in the input text it add extra white space at first char. I really don't know how to remove the extra white space, and when I add ToString(Trim())

<input type="Text" value="#ToString(Trim(convertToNumber(separator, separatorNumber[separator])))#">

it still have extra white space

thank you

Malaspina answered 10/8, 2010 at 6:36 Comment(0)
D
15

Have you tried to added output="false" to your function tag?

<cffunction name="convertToNumber" returntype="numeric" output="false">...</cffunction>

Hope that helps.

Debarath answered 10/8, 2010 at 7:3 Comment(2)
Other things you might want to look into are cfsetting (the enablecfoutputonly attribute ) and the suppression whitespace setting in the ColdFusion administrator.Devisable
be careful using the cfsetting method. i have seen this used in some API wrappers and when they crashed, it would really mess up the output on our pages (when it crashed, the cfsetting tag that would re-enable output never executed, so screwed stuff up). i think that output="false" is a much better and more reliable way to go.Gherlein
S
1

I ran into a similar problem, and what worked for me was putting the value returned from the method into a variable and then outputting the variable instead of the return from the method. I didn't format it at all, but somehow that helped.

Selfpropulsion answered 10/6, 2011 at 21:50 Comment(0)
E
0

It's very simple. Put <cfsilent> in your <cffunction>. Need to put <cfsilent> after <cfargument>.

I've wrote about extra space within cffunction in my blog.

<cffunction name="convertToNumber" returntype="numeric">
    <cfargument name="separator" required="Yes" type="string" />
    <cfargument name="number" required="Yes" type="string" />
    <cfsilent>
        <cfset LOCAL.arrSeparator = ["comma", "period", "lakh_crore"] />
        <cfif ARGUMENTS.separator eq "comma" or ARGUMENTS.separator eq "lakh_crore">
            <cfif ListLen(ARGUMENTS.number, ".") eq 2>
                <cfset LOCAL.integral = ListFirst(ARGUMENTS.number, ".") />
                <cfset LOCAL.fractional = ListLast(ARGUMENTS.number, ".") />
            <cfelse>
                <cfset LOCAL.integral = ARGUMENTS.number />
                <cfset LOCAL.fractional = "" />
            </cfif>
            <cfset LOCAL.integral = Replace(LOCAL.integral, ",", "", "ALL") />
            <cfset LOCAL.returnValue = LOCAL.integral />
            <cfif Len(Trim(LOCAL.fractional))>
                <cfset LOCAL.returnValue &= "." & LOCAL.fractional />
            </cfif>
        </cfif>
        <cfif ARGUMENTS.separator eq "period">
            <cfif ListLen(ARGUMENTS.number, ",") eq 2>
                <cfset LOCAL.integral = ListFirst(ARGUMENTS.number, ",") />
                <cfset LOCAL.fractional = ListLast(ARGUMENTS.number, ",") />
            <cfelse>
                <cfset LOCAL.integral = ARGUMENTS.number />
                <cfset LOCAL.fractional = "" />
            </cfif>
            <cfset LOCAL.integral = Replace(LOCAL.integral, ".", "", "ALL") />
            <cfset LOCAL.returnValue = LOCAL.integral />
            <cfif Len(Trim(LOCAL.fractional))>
                <cfset LOCAL.returnValue &= "." & LOCAL.fractional />
            </cfif>
        </cfif>
        <cfreturn LOCAL.returnValue />
    </cfsilent>
</cffunction>

<cfset separatorNumber = StructNew() />
<cfset separatorNumber.comma = "1,234,567,890.123456" />
<cfset separatorNumber.period = "1.234.567.890,123456" />
<cfset separatorNumber.lakh_crore = "1,23,45,67,890.123456" />
<cfoutput>
    <cfloop collection="#separatorNumber#" item="separator">
        <p>
            #separator# : 
            <input type="Text" value="#convertToNumber(separator, separatorNumber[separator])#">
            Length : #Len(convertToNumber(separator, separatorNumber[separator]))# 
        </p>
    </cfloop>
</cfoutput>
Endogamy answered 10/8, 2010 at 6:41 Comment(1)
Dude..!! It's because of you didn't do what I told. Grasp my above coding. I've edited as putting <cfsilent> into <cffunction> as I told you before. Grasp it.Endogamy

© 2022 - 2024 — McMap. All rights reserved.