Arabic numerals into Roman numerals (1)

Source: Internet
Author: User

A recent project has this need. It is trivial to look for such materials for implementation and share it with you by the way.

What is a Roman numerals? Roman numerals are the earliest digital representation, more than 2000 years earlier than Arabic numerals, originated in Rome. Today, the most common roman numerals are the dial symbol of the clock: I, II, III, IV, V, VI, VII, VIII, IX, x, Jun, Jun ......Corresponding to Arabic numerals (which are commonly used internationally), that is, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12. Arabic numerals were actually invented by ancient Indians. Later they were introduced to Europe by the Arab, and were mistakenly referred to as Arabic numerals by Europeans. The basic characters of the Romo digit notation are: I, V, X, L, C, D, and M. The corresponding Arabic numerals are: 1. If the numbers 5, 10, 50, 100, 500, and 1000 (1) are the same, the numbers expressed are equal to the numbers obtained by the addition of these numbers, for example, Ⅲ = 3; (2) A small number is on the right of a large number. The number is equal to the sum of these numbers, for example, VIII = 8; hour = 12; (3) A small number (limited to I, X, and C) is on the left of a large number. The number indicated by this number is equal to the number of A large number reduced, for example, IV = 4; IX = 9; (4) during normal use, the number repeatedly written must not exceed three times. (The exception of four o'clock "IIII" on the dial) (5) Draw a horizontal line on a number, indicating that the number is increased by 1000 times. Rule of number of groups (1) any of the basic numbers I, X, and C can constitute the number itself, or the number can be formed by the combination on the right of the large number; you can only use one value to the left of a large number. (2) do not place any of the basic numbers V, L, and D as decimal places on the left of a large number and subtract them to form the number. add them to the right of a large number to form the number, only one instance can be used. (3) the small numbers on the left of V and X can only be I. (4) the small numbers on the left of L and C can only use X. (5) the small numbers on the left of D and M can only use C. Algorithm Implementation

The code is implemented using JS. The following is a good example:

Arabit2Roman: function(arabic){        var alpha:[ 'I', 'V', 'X', 'L', 'C', 'D', 'M' ];        var roman="";        var bit = 0;        while (arabic > 0)          {              var tempnum = arabic % 10;            switch (tempnum)              {                  case 3:                  {                       roman=this.alpha[bit]+roman;                      tempnum--;                }                  case 2:                  {                      roman=this.alpha[bit]+roman;                      tempnum--;                }                  case 1:                  {                      roman=this.alpha[bit]+roman;                      break;                  }                  case 4:                  {                      roman=this.alpha[bit + 1]+roman;                    roman=this.alpha[bit]+roman;                      break;                  }                  case 8:                  {                      roman=this.alpha[bit]+roman;                     tempnum--;                }                  case 7:                  {                      roman=this.alpha[bit]+roman;                     tempnum--;                }                  case 6:                  {                      roman=this.alpha[bit]+roman;                      tempnum--;                }                  case 5:                  {                      roman=this.alpha[bit + 1]+roman;                      break;                  }                  case 9:                  {                     roman=this.alpha[bit + 2]+roman;                     roman=this.alpha[bit]+roman;                     break;                  }                  default:                  {                      break;                  }              }              bit += 2;              arabic = Math.floor(arabic / 10);          }       return roman;    }

 

 

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.