Open In App

Converting Roman to Decimal in Python using gnboorse-rom module

Last Updated : 21 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

A gnboorse-rom module is that library of python which helps you convert roman numerals into decimal number and decimal number into roman numerals .It will do conversion of values upto 3999 both in roman numerals and decimal system .

Note: Following is the list of some unique Roman symbols with their corresponding decimal values.

SYMBOL        VALUE
I      ->       1
IV     ->       4
V      ->       5
IX     ->       9
X      ->       10
XL     ->       40
L      ->       50
XC     ->       90
C      ->       100
CD     ->       400
D      ->       500
CM     ->       900 
M      ->       1000       

A number in Roman Numerals is a string of these symbols written in descending order(e.g. M’s first, followed by D’s, etc.). However, in a few specific cases, to avoid four characters being repeated in succession (such as IIII or XXXX), subtractive notation is often used as follows:

  • I placed before V or X indicates one less, so four is IV (one less than 5) and 9 is IX (one less than 10).
  • X placed before L or C indicates ten less, so forty is XL (10 less than 50) and 90 is XC (ten less than a hundred).
  • C placed before D or M indicates a hundred less, so four hundred is CD (a hundred less than five hundred) and nine hundred is CM (a hundred less than a thousand).

Installing Library

This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal.

pip install gnboorse-rom

Functions of gnboorse-rom :

  • 1-rom_generate :: It will convert our decimal number into roman numeral and will return a roman numeral string as a output .
    Example :




    # Importing rom_generate function  
    # From gnboorse's rom Library  
    from rom import rom_generate
      
    a = rom_generate(1)
    print(a)
      
    a = rom_generate(5)
    print(a)
      
    a = rom_generate(15)
    print(a)
      
    a = rom_generate(255)
    print(a)
      
    a = rom_generate(512)
    print(a)
      
    a = rom_generate(786)
    print(a)
      
    a = rom_generate(1444)
    print(a)
      
    a = rom_generate(2000)
    print(a)
      
    a = rom_generate(3999)
    print(a)
    type(a)

    
    

    Output :

    I
    V
    XV
    CCLV
    DXII
    DCCLXXXVI
    MCDXLIV
    MM
    MMMCMXCIX
    str
    
  • 2-rom_parse:It will convert our roman numeral into decimal number and will return a decimal integer number as a output .
    Example :




    # Importing rom_parse function  
    # From gnboorse's rom Library  
    from rom import rom_parse
      
    a = rom_parse('I')
    print(a)
      
    a = rom_parse('XV')
    print(a)
      
    a = rom_parse('L')
    print(a)
      
    a = rom_parse('CCLV')
    print(a)
      
    a = rom_parse('CD')
    print(a)
      
    a = rom_parse('MXII')
    print(a)
      
    a = rom_parse('DDD')
    print(a)
      
    a = rom_parse('MMD')
    print(a)
      
    a = rom_parse('MMMCMXCIX')
    print(a)
    type(a)

    
    

    Output :

    1
    15
    50
    255
    400
    1012
    1500
    2500
    3999
    int
    

Note: For understanding of code behind the logic you can refer to GeeksforGeeks



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads