tommyphan25
Landing Page Strategist
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.
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.
⚙ 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
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.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
Javascript - Convert Number To Words</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
>
- <div
class
=
"col-md-2"
></
div
>
- <div
class
=
"col-md-8 well"
>
- <div
class
=
"form-group"
>
- <input
type
=
"text"
class
=
"form-control"
maxlength
=
"6"
onkeypress
=
"return NumberInput(this.value);"
onkeyup
=
"NumberToWords(this.value);"
/
>
- </
div
>
- <center
><div
id
=
"result"
></
div
></
center
>
- </
div
>
- </
div
>
- </
body
>
- <script
src
=
"js/script.js"
></
script
>
- </
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.
- function
NumberInput(
evt)
{
- var
e =
event ||
evt;
- var
charCode =
e.which
||
e.keyCode
;
- if
(
charCode >
31
&&
(
charCode <
48
||
charCode >
57
)
)
- return
false
;
- return
true
;
- }
- function
NumberToWords(
number)
{
- var
num =
new
String
(
number)
;
- var
splt =
num.split
(
""
)
;
- var
newNumber =
splt.reverse
(
)
;
- var
first =
[
'Zero'
,
'One'
,
'Two'
,
'Three'
,
'Four'
,
'Five'
,
'Six'
,
'Seven'
,
'Eight'
,
'Nine'
]
;
- var
two =
[
'Ten'
,
'Eleven'
,
'Twelve'
,
'Thirteen'
,
'Fourteen'
,
'Fifteen'
,
'Sixteen'
,
'Seventeen'
,
'Eighteen'
,
'Nineteen'
]
;
- var
tens =
[
''
,
'Ten'
,
'Twenty '
,
'Thirty '
,
'Forty '
,
'Fifty '
,
'Sixty '
,
'Seventy '
,
'Eighty '
,
'Ninety '
]
;
- var
word =
new
Array
(
)
;
- var
count =
0
;
- for
(
i=
0
;
i <
newNumber.length
;
i++
)
{
- switch
(
i)
{
- case
0
:
- if
(
(
newNumber[
i]
==
0
)
||
(
newNumber[
i +
1
]
==
1
)
)
{
- word[
count]
=
''
;
- }
- else
{
- word[
count]
=
first[
newNumber[
i]
]
;
- }
- word[
count]
=
word[
count]
;
- break
;
- case
1
:
- Tens(
)
;
- break
;
- case
2
:
- if
(
newNumber[
i]
==
0
)
{
- word[
count]
=
''
;
- }
- else
if
(
(
newNumber[
i -
1
]
==
0
)
||
(
newNumber[
i -
2
]
==
0
)
)
{
- word[
count]
=
first[
newNumber[
i]
]
+
" Hundred "
;
- }
- else
{
- word[
count]
=
first[
newNumber[
i]
]
+
" Hundred "
;
- }
- break
;
- case
3
:
- if
(
newNumber[
i]
==
0
||
newNumber[
i +
1
]
==
1
)
{
- word[
count]
=
''
;
- }
- else
{
- word[
count]
=
first[
newNumber[
i]
]
;
- }
- if
(
(
newNumber[
i +
1
]
!=
0
)
||
(
newNumber[
i]
>
0
)
)
{
- word[
count]
=
word[
count]
+
" Thousand "
;
- }
- break
;
- case
4
:
- Tens(
)
;
- break
;
- case
5
:
- if
(
(
newNumber[
i]
==
0
)
||
(
newNumber[
i +
1
]
==
1
)
)
{
- word[
count]
=
''
;
- }
- else
{
- word[
count]
=
first[
newNumber[
i]
]
;
- }
- if
(
newNumber[
i +
1
]
!==
'0'
||
newNumber[
i]
>
'0'
)
{
- if
(
newNumber[
4
]
===
"0"
&&
newNumber[
3
]
===
'0'
)
{
- word[
count]
=
word[
count]
+
" Hundred Thousand "
;
- }
else
{
- word[
count]
=
word[
count]
+
" Hundred "
;
- }
- }
- break
;
- default
:
break
;
- }
- count++;
- }
- function
Tens(
)
{
- if
(
newNumber[
i]
==
0
)
{
- word[
count]
=
''
;
- }
else
if
(
newNumber[
i]
==
1
)
{
- word[
count]
=
two[
newNumber[
i -
1
]
]
;
- }
else
{
- word[
count]
=
tens[
newNumber[
i]
]
;
- }
- }
- word.reverse
(
)
;
- var
result =
''
;
- for
(
i=
0
;
i <
newNumber.length
;
i++
)
{
- result =
result +
word[
i]
;
- }
- document.getElementById
(
'result'
)
.innerHTML
=
result;
- }
⚙ 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.