Converting Roman to Decimal in Python using gnboorse-rom module

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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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)

    chevron_right

    
    

    Output :

    1
    15
    50
    255
    400
    1012
    1500
    2500
    3999
    int
    

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.