Community
    • Login

    How to: Convert Roman numerals to Arabic numbers ?

    Scheduled Pinned Locked Moved General Discussion
    4 Posts 2 Posters 1.6k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • vmars vernonV Offline
      vmars vernon
      last edited by

      I have an Html document that sometimes uses Roman numerals
      and I want to Convert Roman numerals to Arabic numbers .
      How can I do this ?
      Thanks for your Help…

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP Online
        PeterJones @vmars vernon
        last edited by

        @vmars-vernon ,

        Notepad++ doesn’t know roman numerals as distinct from any other character, and has no inherent meaning associated. Most of the roman-to-arabic conversions I know use math, and Notepad++ search/replace cannot do math.

        You would either have to find or write a plugin that could do it, or use a plugin like PythonScript and write a script that would do the conversion.

        When making the plugin or script, just make sure you think about edge cases like <blockquote>I came, I saw, I conquered</blockquote> (which should not become <blockquote>1 came, 1 saw, 1 conquered</blockquote>), or <p>Use Notepad++, not vi, if you're on Windows</p> (which should not become <p>Use Notepad++, not 6, if you're on Windows</p>)

        PeterJonesP 1 Reply Last reply Reply Quote 1
        • PeterJonesP Online
          PeterJones @PeterJones
          last edited by

          @vmars-vernon ,

          I got interested:

          # encoding=utf-8
          """in response to https://community.notepad-plus-plus.org/topic/21611/
          
          matches any "roman numeral", which I define as
          * any "word" that's a combination of I, V, X, L, C, D, M (only upper case)
          * except if it's a lone I at the beginning of a line,
            or a lone I after a . or ? or ! or ; or , or " and one or two spaces
          """
          from Npp import *
          
          def roman_to_int(s):
              """stolen from https://www.w3resource.com/python-exercises/class-exercises/python-class-exercise-2.php"""
              rom_val = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
              int_val = 0
              for i in range(len(s)):
                  if i > 0 and rom_val[s[i]] > rom_val[s[i - 1]]:
                      int_val += rom_val[s[i]] - 2 * rom_val[s[i - 1]]
                  else:
                      int_val += rom_val[s[i]]
              return int_val
          
          def deromanize_match(m):
              return str(roman_to_int(m.group().rstrip()))
          
          editor.beginUndoAction()
          editor.rereplace(r'(?-is)(?!^\x20*I\x20)(?!(?<=[.?!,;"]\x20)I\x20)(?!(?<=[.?!,;"]\x20\x20)I\x20)\b[IVXLCDM]+\b', deromanize_match)
          editor.endUndoAction()
          

          converts

          Convert MMMCMLXXXVI
          Convert MDCLXVI
          I say, I am not sure, but treat this line as pronouns
          Don't edit using the vi editor, which won't match because it's lower case
          Some sentence. I am still a sentence with a pronoun.
          But this.  The I here is a roman numeral.
          I say, II is surely roman.
          

          to

          Convert 3986
          Convert 1666
          I say, I am not sure, but treat this line as pronouns
          Don't edit using the vi editor, which won't match because it's lower case
          Some sentence. I am still a sentence with a pronoun.
          But this.  The 1 here is a roman numeral.
          I say, 2 is surely roman.
          
          1 Reply Last reply Reply Quote 5
          • vmars vernonV Offline
            vmars vernon
            last edited by

            Whoa!
            Thank you very much Peter .
            Here is the .html :
            [https://vmars.us/ShowMe/A.Little.Book-On-the-Christian-Life-John-Calvin-Direct-Edit.html](link url)
            …Vernon

            1 Reply Last reply Reply Quote 0

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors