Coldfusion奇怪的额外空间

发布于 2024-09-13 19:39:41 字数 2682 浏览 5 评论 0原文

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

<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])))#">

它仍然有多余的空白,

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

子栖 2024-09-20 19:39:41

您是否尝试过将 output="false" 添加到函数标记中?

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

希望有帮助。

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

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

Hope that helps.

离去的眼神 2024-09-20 19:39:41

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

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.

烦人精 2024-09-20 19:39:41

这很简单。将 放入 中。需要将 放在 之后。

我在我的博客中写了关于 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文