2010-02-10 94 views
4

我有一个coldfusion应用程序,我在其中计算某个对象的剩余数量。Coldfusion将数字转换为文本

所以我有一个整数......像9

但我需要把它打印到屏幕上以文字形式....像九岁。

有没有内置函数来做到这一点?我Google搜索,找不到一个。

回答

8

史蒂芬是正确的,直接的答案是,有没有内置此功能,但这里是一个UDF可以用NumberAsString

+0

你知道我在发表我的评论之前曾经在CFLib上搜索过高和低,但是找不到它并最终放弃了。 – 2010-02-12 15:04:47

1

不,我恐怕没有内置的功能。

您需要在cfc中编写用户定义的函数或方法来为您执行此操作。

1

起到以下是我做到了,经过多方努力,我想补充。我是在ColdFusion 4.5下编写的,所以你的里程可能会有所不同。

关键是将数字分成3个数字块,然后用各自的addenuum(百万,千等)显示每个块,当然,还要处理青少年中的随机零和数字。第一个列表可用于更改语言,但要求订单正确(即第一个条目为= 1)。这是一个检查显示,其中的数字必须用英文写出,因此有很多'检查'变量。你正在转换的变量是一个名为'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>