num2str
Example Output
(Refreshing page will change number example)
  1. 0 or zero
  2. 1 or one
  3. 552 or five hundred fifty-two
  4. 78,011,469 or seventy-eight million eleven thousand four hundred sixty-nine
  5. 871,680,329 or eight hundred seventy-one million six hundred eighty thousand three hundred twenty-nine
  6. -23,475,393 or negative twenty-three million four hundred seventy-five thousand three hundred ninety-three
        
Source code <cfset Example = structNew()> <cfset Example[1] = 0> <cfset Example[2] = randRange(1,10)> <cfset Example[3] = randRange(1,1000)> <cfset Example[4] = randRange(100000,99999999)> <cfset Example[5] = randRange(100000000,2100000000)> <cfset Example[6] = randRange(-100000000,0)> <br /> (Refreshing page will change number example) <ol> <cfoutput> <cfloop from="1" to="#structCount(Example)#" index="Example.I"> <li>#numberFormat(Example[Example.I],",")# or <strong>#num2str(Example[Example.I])#</strong></li> </cfloop> </cfoutput> </ol> <cfscript> function num2Str(number) { // Returns a number in word format. // @param number - to be converted (Number) // Returns number in string format (String) // Author Raymond Compton - usaRaydar@gmail.com // 2007.1.1 var v = structNew(); v.NumSets = numberFormat(number,","); v.Count = listLen(v.NumSets); v.Return = ''; // Sets return to blank if ( number lt 0 ) v.Return = 'negative'; // Set negative number else if ( number eq 0 ) return "zero"; // Returns 0 // Number words - Edit to your language needs. v.Tag = "thousand,million,billion,trillion,quadrillion"; v.N1="one";v.N2="two";v.N3="three";v.N4="four";v.N5="five"; v.N6="six";v.N7="seven";v.N8="eight";v.N9="nine";v.N10="ten"; v.N11="eleven";v.N12="twelve";v.N13="thirteen";v.N14="fourteen";v.N15="fifteen"; v.N16="sixteen";v.N17="seventeen";v.N18="eighteen";v.N19="nineteen"; v.N20="twenty";v.N30="thirty";v.N40="forty";v.N50="fifty"; v.N60="sixty";v.N70="seventy";v.N80="eighty";v.N90="ninety"; // for(v.i = 1; v.i lte v.Count; v.i = v.i + 1) { // Loops thru sets of 3 digits v.Words = ''; // Inits words for this set. v.P123 = numberFormat(listGetAt(v.NumSets,v.i),"000"); // all 3 digits if ( val(v.P123) neq 0 ) { // if no value in set v.p1 = left(v.P123,1); // hundreds v.p23 = right(v.P123,2); // tens and ones if ( v.P1 gt 0 ) // if hundreds value exists v.Words = listAppend(v.Words,"#evaluate('v.N#v.P1#')# hundred",' '); // Convert hundreds if ( v.P23 gt 0 ) { // if value below 100 exists if ( v.P23 gte 20 ) { // if 20 or over v.P2 = left(v.P23,1); // split into tens v.P3 = right(v.P23,1); // and ones v.Words = listAppend(v.Words,evaluate('v.N#v.P2#0'),' '); // Convert tens if ( v.P3 gt 0 ) v.Words = listAppend(v.Words,evaluate('v.N#v.P3#'),'-'); // Convert ones } else // else less than 20 - convert as is v.Words = listAppend(v.Words,evaluate('v.N#val(v.P23)#'),' '); // Convert < 20 } if ( v.i neq v.Count ) // Assures tag is inserted only when > 1000 v.Words = listAppend(v.Words,listGetAt(v.Tag,v.Count-v.i),' '); // Insert set's tag (thousands,millions,etc) v.Return = listAppend(v.Return,v.Words,' '); // Adds words for set to return string. } } return v.Return; } </cfscript>
© 2017 usaRaydar
v.
IP:23.20.86.177 css:/assets/css/raycomweb.css?v=16021511