Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I did it, after much effort, I might add. I wrote this under ColdFusion 4.5, so your mileage may vary. </p> <p>The key is breaking the digits into 3-number chunks, then displaying each chunk with the respective addenuum (million, thousands, etc.), and of course, dealing with random zeros and numbers in the teens. The first lists can be used to change languages, but require the order be right (i.e. first entry is = 1). This was for a check display, where the numeral had to be written out in english, therefore the plethora of 'check' variables. The variable you're converting is a numeral named 'check_amount'.</p> <p>I'll apologize up front for the lousy code - I'm a designer, not a programmer. There are so many repeated sections that should be refactored, but it's for dealing with leading zeros and teens, mostly.</p> <p>Third edit is the charm, I suppose. This final (working) version now handles zero dollars properly.</p> <pre><code>&lt;cfoutput&gt;&lt;cfif IsNumeric(check_amount)&gt; &lt;!--- is it a number? ---&gt; &lt;cfparam name="write_single" default="one,two,three,four,five,six,seven,eight,nine, "&gt; &lt;cfparam name="write_double" default=" ,twenty,thirty,fourty,fifty,sixty,seventy,eighty,ninety"&gt; &lt;cfparam name="teens" default="11,12,13,14,15,16,17,18,19"&gt; &lt;cfparam name="teens_written" default="eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen"&gt; &lt;cfparam name="triplet_after" default="hundred, thousand, million, billion, trillion, quadrillion, quintillion, hexillion, heptillion, octillion, nonillion, decillion, unodecillion, duodecillion"&gt; &lt;cfset x=#ListLen(DecimalFormat(check_amount))#&gt; &lt;!--- seperate the number into sections, using the built-in Decimal Format to make it into a list of 3-digit numbers ---&gt; &lt;cfloop list="#DecimalFormat(check_amount)#" index="trips" delimiters=","&gt; &lt;!--- seperate the number into hundreds tens and singles, making the teens exception ---&gt; &lt;cfif #Evaluate(Int(trips))# NEQ "0"&gt; &lt;cfif Len(Int(trips)) EQ "3"&gt; &lt;cfif mid(Int(trips), 1, 1) EQ "0"&gt; &lt;cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0"&gt; #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')# &lt;cfelse&gt; #listGetAt(write_double, mid(Int(trips), 2, 1), ',')# &lt;cfif mid(Int(trips), 3, 1) NEQ "0"&gt; #listGetAt(write_single, mid(Int(trips), 3, 1), ',')# &lt;/cfif&gt; &lt;/cfif&gt; &lt;cfelse&gt; #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# #listGetAt(triplet_after, 1, ',')# &lt;/cfif&gt; &lt;cfif mid(trips, 2, 1) NEQ "0"&gt; &lt;cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0"&gt; #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')# &lt;cfelse&gt; #listGetAt(write_double, mid(Int(trips), 2, 1), ',')# &lt;cfif mid(trips, 3, 1) NEQ "0"&gt; #listGetAt(write_single, mid(Int(trips), 3, 1), ',')# &lt;/cfif&gt; &lt;/cfif&gt; &lt;cfelse&gt; &lt;cfif mid(trips, 3, 1) NEQ "0"&gt; #listGetAt(write_single, mid(Int(trips), 3, 1), ',')# &lt;/cfif&gt; &lt;/cfif&gt; &lt;cfelseif Len(Int(trips)) EQ "2" AND mid(Int(trips), 1, 1) NEQ "0"&gt; &lt;cfif #ListFind(teens, right(Int(trips), 2), ',')# NEQ "0"&gt; #listGetAt(teens_written, ListFind(teens, right(int(trips), 2), ','), ',')# &lt;cfelse&gt; #listGetAt(write_double, mid(Int(trips), 1, 1), ',')# &lt;cfif mid(trips, 2, 1) NEQ "0"&gt; #listGetAt(write_single, mid(Int(trips), 2, 1), ',')# &lt;/cfif&gt; &lt;/cfif&gt; &lt;cfelseif Len(Int(trips)) EQ 1 AND mid(int(trips), 1, 1) NEQ "0"&gt; #listGetAt(write_single, mid(Int(trips), 1, 1), ',')# &lt;/cfif&gt; &lt;!--- deal with the thousands and millions seperators, doesn't include hundreds on last loop ---&gt; &lt;cfif x NEQ "1"&gt;#listGetAt(triplet_after, x, ',')#&lt;/cfif&gt; &lt;cfelse&gt; &lt;!--- Zero Dollars? How about... ---&gt;&lt;cfif x EQ #ListLen(DecimalFormat(check_amount))#&gt; No &lt;/cfif&gt; &lt;/cfif&gt; &lt;cfset x=x-1&gt;&lt;!--- next loop, next valuations ---&gt; &lt;/cfloop&gt; &lt;!--- output tailing text and cents in check format ---&gt; Dollars and &lt;sup&gt;#right(DecimalFormat(check_amount), 2)#&lt;/sup&gt;/&lt;sub&gt;100&lt;/sub&gt; cents&lt;/p&gt; &lt;/cfif&gt;&lt;/cfoutput&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload