Advertisement
KimKat

Number to English word functionaliy

Mar 23rd, 2013
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. After I saw the post [http://pastebin.com/susqS9NG] I decided to write my own JavaScript function that does the same thing as it does in Ruby but for JavaScript. It's quite useful for digit to word & word to digit conversions (I guess).
  3.  
  4. Notice: To avoid collisions, remove "Ten" from the array and keep it only at "Nine" then it will function correctly.
  5. If you want you could extend it further to translate more numbers into words but you'd be more or less required to perform quite excessive strict checks for whether the value is what it claims to be, so that's the reason why I recommend to keep it at 9 basically. I'm assuming that the array numbers don't really have any conflicts. ^^
  6.  
  7. The main reason I wrote this JavaScript was because the HUGE loads of lines it takes to write it in Ruby. I almost laughed because it can be written much easier if you just find or create "replace" functionality in Ruby. I'm quite sure. Hence why I created my own JavaScript function. Enjoy! =)
  8. */
  9.  
  10. function BasicNumToWord(n) {
  11. a=new Array(0,1,2,3,4,5,6,7,8,9,10);
  12. return(n.replace(a[0],"Zero").replace(a[1],"One").replace(a[2],"Two").replace(a[3],"Three").replace(a[4],"Four").replace(a[5],"Five").replace(a[6],"Six").replace(a[7],"Seven").replace(a[8],"Eight").replace(a[9],"Nine").replace(a[10],"Ten"));
  13. }
  14. BasicNumToWord("012345678910");
  15.  
  16. // Return value: "ZeroOneTwoThreeFourFiveSixSevenEightNineTen"
  17. // Basic way to write numbers to words.
  18.  
  19. function BinNumToWord(n) {
  20. return(n.replace(/0/g,"Zero").replace(/1/g,"One").replace(/2/g,"Two").replace(/3/g,"Three").replace(/4/g,"Four").replace(/5/g,"Five").replace(/6/g,"Six").replace(/7/g,"Seven").replace(/8/g,"Eight").replace(/9/g,"Nine").replace(/10/g,"Ten"));
  21. }
  22. BinNumToWord("00011111");
  23.  
  24. // Return value: "ZeroZeroZeroOneOneOneOneOne"
  25. // You could use this function to convert data to or from binary. This include the ability to create custom prefixes such as the Zero's & One's, so it's pretty unique.
  26.  
  27. function NumToWord(n) {
  28. a=new Array(0,1,2,3,4,5,6,7,8,9,10,100,1000,1337,13337,133337,1333337);
  29. return(n.replace(a[0],"Zero").replace(a[1],"One").replace(a[2],"Two").replace(a[3],"Three").replace(a[4],"Four").replace(a[5],"Five").replace(a[6],"Six").replace(a[7],"Seven").replace(a[8],"Eight").replace(a[9],"Nine").replace(a[10],"Ten").replace(a[11],"Hundred").replace(a[12],"Thousand").replace(a[13],"One thousand three hundred thirty seven").replace(a[14],"Ten thousand three hundred thirty seven").replace(a[15],"Hundred thousand three hundred thirty seven").replace(a[16],"One million three hundred thirty three thousand three hundred thirty seven"));
  30. }
  31. NumToWord("01234567891010010001337133371333371333337");
  32.  
  33. // Return value: "ZeroOneTwoThreeFourFiveSixSevenEightNineTenHundredThousandOne thousand three hundred thirty sevenTen thousand three hundred thirty sevenHundred thousand three hundred thirty sevenOne million three hundred thirty three thousand three hundred thirty seven"
  34. // More advanced way of writing numbers to words.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement