• We just launched and are currently in beta. Join us as we build and grow the community.

Javascript - Convert Number To Words

tommyphan25

Landing Page Strategist
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
137
Likes
61
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
In this tutorial we will create a Convert Number To Words using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

This is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav>
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    Javascript - Convert Number To Words</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    >
  17. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  18. <div

    class

    =

    "col-md-8 well"

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <input

    type

    =

    "text"

    class

    =

    "form-control"

    maxlength

    =

    "6"

    onkeypress

    =

    "return NumberInput(this.value);"

    onkeyup

    =

    "NumberToWords(this.value);"

    /

    >
  21. </

    div

    >
  22. <center

    ><div

    id

    =

    "result"

    ></

    div

    ></

    center

    >
  23. </

    div

    >
  24. </

    div

    >
  25. </

    body

    >
  26. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  27. </

    html

    >

Creating the Script

This code contains the script of the application.This code will convert the number into words and display it the page everytime the user enter some numbers. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. function

    NumberInput(

    evt)

    {
  2. var

    e =

    event ||

    evt;
  3. var

    charCode =

    e.which

    ||

    e.keyCode

    ;

  4. if

    (

    charCode >

    31

    &&

    (

    charCode <

    48

    ||

    charCode >

    57

    )

    )
  5. return

    false

    ;
  6. return

    true

    ;
  7. }

  8. function

    NumberToWords(

    number)

    {
  9. var

    num =

    new

    String

    (

    number)

    ;
  10. var

    splt =

    num.split

    (

    ""

    )

    ;
  11. var

    newNumber =

    splt.reverse

    (

    )

    ;
  12. var

    first =

    [

    'Zero'

    ,

    'One'

    ,

    'Two'

    ,

    'Three'

    ,

    'Four'

    ,

    'Five'

    ,

    'Six'

    ,

    'Seven'

    ,

    'Eight'

    ,

    'Nine'

    ]

    ;
  13. var

    two =

    [

    'Ten'

    ,

    'Eleven'

    ,

    'Twelve'

    ,

    'Thirteen'

    ,

    'Fourteen'

    ,

    'Fifteen'

    ,

    'Sixteen'

    ,

    'Seventeen'

    ,

    'Eighteen'

    ,

    'Nineteen'

    ]

    ;
  14. var

    tens =

    [

    ''

    ,

    'Ten'

    ,

    'Twenty '

    ,

    'Thirty '

    ,

    'Forty '

    ,

    'Fifty '

    ,

    'Sixty '

    ,

    'Seventy '

    ,

    'Eighty '

    ,

    'Ninety '

    ]

    ;

  15. var

    word =

    new

    Array

    (

    )

    ;
  16. var

    count =

    0

    ;

  17. for

    (

    i=

    0

    ;

    i <

    newNumber.length

    ;

    i++

    )

    {
  18. switch

    (

    i)

    {
  19. case

    0

    :
  20. if

    (

    (

    newNumber[

    i]

    ==

    0

    )

    ||

    (

    newNumber[

    i +

    1

    ]

    ==

    1

    )

    )

    {
  21. word[

    count]

    =

    ''

    ;
  22. }
  23. else

    {
  24. word[

    count]

    =

    first[

    newNumber[

    i]

    ]

    ;
  25. }

  26. word[

    count]

    =

    word[

    count]

    ;

  27. break

    ;

  28. case

    1

    :
  29. Tens(

    )

    ;
  30. break

    ;

  31. case

    2

    :
  32. if

    (

    newNumber[

    i]

    ==

    0

    )

    {
  33. word[

    count]

    =

    ''

    ;
  34. }
  35. else

    if

    (

    (

    newNumber[

    i -

    1

    ]

    ==

    0

    )

    ||

    (

    newNumber[

    i -

    2

    ]

    ==

    0

    )

    )

    {
  36. word[

    count]

    =

    first[

    newNumber[

    i]

    ]

    +

    " Hundred "

    ;
  37. }
  38. else

    {
  39. word[

    count]

    =

    first[

    newNumber[

    i]

    ]

    +

    " Hundred "

    ;
  40. }
  41. break

    ;


  42. case

    3

    :
  43. if

    (

    newNumber[

    i]

    ==

    0

    ||

    newNumber[

    i +

    1

    ]

    ==

    1

    )

    {
  44. word[

    count]

    =

    ''

    ;
  45. }
  46. else

    {
  47. word[

    count]

    =

    first[

    newNumber[

    i]

    ]

    ;
  48. }
  49. if

    (

    (

    newNumber[

    i +

    1

    ]

    !=

    0

    )

    ||

    (

    newNumber[

    i]

    >

    0

    )

    )

    {
  50. word[

    count]

    =

    word[

    count]

    +

    " Thousand "

    ;
  51. }
  52. break

    ;

  53. case

    4

    :
  54. Tens(

    )

    ;
  55. break

    ;

  56. case

    5

    :
  57. if

    (

    (

    newNumber[

    i]

    ==

    0

    )

    ||

    (

    newNumber[

    i +

    1

    ]

    ==

    1

    )

    )

    {
  58. word[

    count]

    =

    ''

    ;
  59. }
  60. else

    {
  61. word[

    count]

    =

    first[

    newNumber[

    i]

    ]

    ;
  62. }
  63. if

    (

    newNumber[

    i +

    1

    ]

    !==

    '0'

    ||

    newNumber[

    i]

    >

    '0'

    )

    {

  64. if

    (

    newNumber[

    4

    ]

    ===

    "0"

    &&

    newNumber[

    3

    ]

    ===

    '0'

    )

    {
  65. word[

    count]

    =

    word[

    count]

    +

    " Hundred Thousand "

    ;
  66. }

    else

    {
  67. word[

    count]

    =

    word[

    count]

    +

    " Hundred "

    ;
  68. }
  69. }

  70. break

    ;


  71. default

    :

    break

    ;
  72. }

  73. count++;

  74. }


  75. function

    Tens(

    )

    {
  76. if

    (

    newNumber[

    i]

    ==

    0

    )

    {
  77. word[

    count]

    =

    ''

    ;
  78. }

    else

    if

    (

    newNumber[

    i]

    ==

    1

    )

    {
  79. word[

    count]

    =

    two[

    newNumber[

    i -

    1

    ]

    ]

    ;
  80. }

    else

    {
  81. word[

    count]

    =

    tens[

    newNumber[

    i]

    ]

    ;
  82. }
  83. }

  84. word.reverse

    (

    )

    ;
  85. var

    result =

    ''

    ;
  86. for

    (

    i=

    0

    ;

    i <

    newNumber.length

    ;

    i++

    )

    {
  87. result =

    result +

    word[

    i]

    ;
  88. }


  89. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    result;
  90. }

⚙ Live Demo

There you have it we successfully created a Convert Number To Words using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,891

323,899

Top