javascript--converts a decimal number to a Roman numeral display

Source: Internet
Author: User



Afternoon on the FCC (freecodecamp) Chinese online to do an exercise: the given number into Roman numerals . Tortured for one hours, finally can give the basic ability to achieve. The process is as follows:



About Roman Numerals



A detailed description of the Roman numerals can be found in Baidu, or Roman numerals. Here is a brief introduction (the picture is selected from Roman numerals):



1. Roman numerals use 7 Roman letters (uppercase) as numbers, representing the following decimal numbers:



          



some numbers are represented by Roman numerals:



                 



2. Counting rules for Roman numerals:



(1) The same number ligatures, the value is added, such as III = 3;



(2) The small number on the right side of the large number, is also cumulative, such as VI = 6;



(3) The small number on the left side of the large number, resulting in a large number of decreases, such as IV = 4;



(4) A horizontal line is drawn above the number, indicating an enlargement of 1000 times times.



A simple and rude example:



            



However, we will encounter some special situations:



(1) 3999 = mmmcmxcix,3999 = MMMDCCCCLXXXXVIIII, both of the results are 3999, but we generally choose simple, that is, the former;



(2) Similarly, 3444 = Mmmcdxliv, 3444 = MMMCCCCXXXXIIII, is also the way to choose the former.



So, first put 1~9 the situation to enumerate:






It can be seen that the Roman numerals on the treatment of 4 and 9 are different with others.



List 4, 9 related parts of the figures:








Ideas and code implementation



1. First convert the numbers into corresponding Roman letters, such as 3999 = MMMDCCCCLXXXXVIIII;



2. Replace the 4 and 9 cases by replacing () to make 3999 = Mmmcmxcix.





1 function convert (num) {
 2 var newArr = [];
 3 var newStr;
 4 // Convert numbers to corresponding Roman letters first
 5 while (num> 0) {
 6 if (num-1000> = 0) {
 7 newArr.push (‘M’);
 8 num-= 1000;
 9} else if (num-500> = 0) {
10 newArr.push (‘D’);
11 num-= 500;
12} else if (num-100> = 0) {
13 newArr.push (‘C’);
14 num-= 100;
15} else if (num-50> = 0) {
16 newArr.push (‘L’);
17 num-= 50;
18} else if (num-10> = 0) {
19 newArr.push (‘X’);
20 num-= 10;
21} else if (num-5> = 0) {
22 newArr.push (‘V’);
23 num-= 5;
24} else if (num-1> = 0) {
25 newArr.push (‘I’);
26 num-= 1;
27}
28}
29 newStr = newArr.join (‘‘);
30 // Replace the 4 and 9 cases
31 newStr = newStr.replace (/ VI {4} | LX {4} | DC {4} | I {4} | X {4} | C {4} / g, function (match) {
32 switch (match) {
33 case ‘VIIII’:
34 return "IX";
35 case ‘LXXXX’:
36 return "XC";
37 case ‘DCCCC’:
38 return "CM";
39 case ‘IIII’:
40 return "IV";
41 case ‘XXXX’:
42 return "XL";
43 case ‘CCCC’:
44 return "CD";
45}
46});
47 return newStr;
48} 





Test some numbers:


console.log(convert(3999));    // MMMCMXCIX
console.log(convert(3444));    // MMMCDXLIV
console.log(convert(1234));   // MCCXXXIV 
console.log(convert(83));    // LXXXIII 
console.log(convert(123));   // CXXIII


At this point, the basic functionality has been implemented.



In addition, the first thousands of smaller numbers are tested with a for loop, which can be converted normally, and the larger number will be longer because there is no processing of 5000,10000.






javascript--converts a decimal number to a Roman numeral display


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.