coldfusion奇怪的额外空间

时间:2010-08-10 06:36:44

标签: coldfusion cfc

我有一个将字符串转换为数字

的功能
<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>

长度为17,但在输入文本中,它会在第一个字符处添加额外的空格。我真的不知道如何删除多余的空格,当我添加ToString(Trim())

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

它仍然有额外的空白

谢谢

3 个答案:

答案 0 :(得分:14)

您是否尝试将output="false"添加到功能标记中?

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

希望有所帮助。

答案 1 :(得分:1)

我遇到了类似的问题,对我有用的是将方法返回的值放入变量,然后输出变量而不是方法的返回值。我根本没有格式化它,但不知何故有帮助。

答案 2 :(得分:0)

这很简单。将<cfsilent>放入<cffunction>。需要在<cfsilent>之后添加<cfargument>

我在博客中写过关于cffunction的额外空间。

<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>