How to Convert Numeric into Roman Numbers in JavaScript

How to Convert Numeric into Roman Numbers in JavaScript

Introduction

In this tutorial we will create a How to Convert Numeric into Roman Numbers in JavaScript. This tutorial purpose is to teach you how to you can convert numeric into roman numbers. This will cover all the basic function will change your background I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any application that uses numbers for displaying result. I will give my best to provide you the easiest way of creating this program Convert numeric to roman numeral. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface to our application. This code will only display the textbox and buttons in the form. To create this simply copy and write it into your text editor, then save it 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. <script type="text/javascript" language="Javascript" src="code_js.js"></script>
  7. </head>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">How to Convert Numeric into Roman Numbers</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-5">
  18. <div class="form-group">
  19. <label>Enter a number</label>
  20. <input type="number" id="number" class="form-control"/>
  21. <br />
  22. <button class="btn btn-primary" onclick="convertToRoman();">Convert</button>
  23. <button class="btn btn-success" onclick="reset();">Reset</button>
  24. </div>
  25. </div>
  26. <div class="col-md-6">
  27. <h4>=ROMAN NUMERAL=</h4>
  28. <div id="result"></div>
  29. </div>
  30. </div>
  31. <script src="script.js"></script>
  32. </body>
  33. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will convert the entered numeric into roman number when the button is clicked. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function numericToRoman(val) {
  2. var roman=['I', 'IV', 'V', 'IX', 'X', 'XL', 'L', 'XC', 'C', 'CD', 'D', 'CM', 'M'];
  3. var numbers=[1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
  4. var x = parseInt(val);
  5.  
  6. if( x<1 || x>4999 ){
  7. alert(x+' is invalid number.');
  8. return 0;
  9. }
  10. var result = "";
  11. for(i=12; i>=0;)
  12. if(numbers[i]<=x ){
  13. result += roman[i];
  14. x -= numbers[i];
  15. }
  16. else{
  17. i--;
  18. }
  19. return(result);
  20. }
  21.  
  22. function convertToRoman(){
  23. if(document.getElementById('number').value != ""){
  24. var number = parseInt(document.getElementById('number').value);
  25.  
  26. if(numericToRoman(number) !== 0){
  27. document.getElementById('result').innerHTML = "<label style='font-size:20px;'><span style='font-size:25px;' class='text-danger'>"+number+"</span> is equal to <span style='font-size:30px;' class='text-primary'>"+numericToRoman(number)+"</span></label>";
  28. }else{
  29. document.getElementById('result').innerHTML = "";
  30. }
  31. }else{
  32. alert("Please enter a number first!");
  33. }
  34.  
  35. }
  36.  
  37. function reset(){
  38. document.getElementById('number').value = "";
  39. document.getElementById('result').innerHTML = "";
  40. }

In the code above we first create a method called numericToRoman(), this function will set all the equal values of the roman numbers to numeric values. This method uses several functionalities including loops and conditional statement in order to match the number you have entered. And next we will create a method that will display the pre-convert numeric number into the html page content, we will call this method convertToRoman().

That' its we just successfully create the application that can convert the numeric into roman numbers, try to run the application and see if it works.

Output:

The How to Convert Numeric into Roman Numbers in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Convert Numeric into Roman Numbers in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment