Coldfusion数字转文本

6

我有一个使用ColdFusion开发的应用程序,其中我计算了某个对象剩余的数量。

我有一个整数…比如说9。

但我需要将其以文本形式打印到屏幕上……比如说“九”。

是否有内置函数可以实现这一点?我已经在Google上搜索了,但找不到相关的信息。

3个回答

9

Steven说得对,直接回答是没有内置函数可以实现这个功能,但是这里有一个UDF可以供您参考NumberAsString


你知道在发表评论之前我在 CFLib 上四处搜寻,但是找不到,最终放弃了。 - Stephen Moretti

1

这是我花费了很多精力才完成的,我写在 ColdFusion 4.5 下,所以你的情况可能会有所不同。

关键是将数字分成三位数块,然后显示每个块与相应的附加项(百万、千位等),当然,还要处理随机的零和十几个数字。第一个列表可用于更改语言,但需要正确排序(即第一个条目=1)。这是为了显示支票而进行的,其中数字必须用英语书写,因此出现了大量的“check”变量。您要转换的变量是名为“check_amount”的数字。

我要提前为糟糕的代码道歉-我是一名设计师,而不是程序员。有许多重复的部分应该重构,但主要是用于处理前导零和十几岁的问题。

第三次编辑就是魅力所在。这个最终(工作)版本现在可以正确处理零美元。

<cfoutput><cfif IsNumeric(check_amount)> <!--- is it a number? --->
<cfparam name="write_single" default="one,two,three,four,five,six,seven,eight,nine, ">
<cfparam name="write_double" default=" ,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninety">
<cfparam name="teens" default="11,12,13,14,15,16,17,18,19">
<cfparam name="teens_written" default="eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen">
<cfparam name="triplet_after" default="hundred, thousand, million, billion, trillion, quadrillion, quintillion, hexillion, heptillion, octillion, nonillion, decillion, unodecillion, duodecillion">

<cfset x=#ListLen(DecimalFormat(check_amount))#>
<!--- seperate the number into sections, using the built-in Decimal Format to make it into a list of 3-digit numbers --->
<cfloop list="#DecimalFormat(check_amount)#" index="trips" delimiters=",">
<!--- seperate the number into hundreds tens and singles, making the teens exception --->
<cfif #Evaluate(Int(trips))# NEQ "0">
    <cfif Len(Int(trips)) EQ "3">
        <cfif mid(Int(trips), 1, 1) EQ "0">
            <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
                #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
            <cfelse>
                #listGetAt(write_double, mid(Int(trips), 2, 1), ',')#
                <cfif mid(Int(trips), 3, 1) NEQ "0">
                    #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
                </cfif>
            </cfif>
        <cfelse>
            #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# #listGetAt(triplet_after, 1, ',')#
        </cfif>
        <cfif mid(trips, 2, 1) NEQ "0">
            <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
                #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
            <cfelse>
                #listGetAt(write_double, mid(Int(trips), 2, 1), ',')#
                <cfif mid(trips, 3, 1) NEQ "0">
                    #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
                </cfif>
            </cfif>
        <cfelse>
            <cfif mid(trips, 3, 1) NEQ "0">
                #listGetAt(write_single, mid(Int(trips), 3, 1), ',')#
            </cfif> 
        </cfif> 
    <cfelseif Len(Int(trips)) EQ "2" AND mid(Int(trips), 1, 1) NEQ "0">
        <cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0">
            #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')#
        <cfelse>
            #listGetAt(write_double, mid(Int(trips), 1, 1), ',')#
            <cfif mid(trips, 2, 1) NEQ "0">
                #listGetAt(write_single, mid(Int(trips), 2, 1), ',')#
            </cfif>
        </cfif>
    <cfelseif Len(Int(trips)) EQ 1 AND mid(int(trips), 1, 1) NEQ "0">
        #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# 
    </cfif>
        <!--- deal with the thousands and millions seperators, doesn't include hundreds on last loop --->
        <cfif x NEQ "1">#listGetAt(triplet_after, x, ',')#</cfif>
<cfelse>
    <!--- Zero Dollars? How about... ---><cfif x EQ #ListLen(DecimalFormat(check_amount))#> No </cfif> 
</cfif>
<cfset x=x-1><!--- next loop, next valuations --->
</cfloop>
<!--- output tailing text and cents in check format --->
Dollars and <sup>#right(DecimalFormat(check_amount), 2)#</sup>/<sub>100</sub> cents</p>
</cfif></cfoutput>

1

不好意思,这方面没有内置函数。

您需要编写一个用户自定义的函数或方法在cfc文件中来实现此功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接