The HotDocs Computation Archive
Get Extra Help

0060 - Convert a Roman Numeral

Description:

Converts a Roman numeral back into a standard (Arabic) number.


• Code •

SET TextVar TO "«TextVar:LIKE THIS»"
SET Temp-n TO LENGTH( TextVar )
SET LoopLimit-n[ Temp-n ] TO 1

0
REPEAT Loop

   // Look at the next two characters
   SET Char-t TO MID( TextVar, COUNTER, 1 )
   IF COUNTER < Temp-n
      SET Next-t TO MID( TextVar, COUNTER + 1, 1)
   ELSE
      SET Next-t TO ""
   END IF

   // 1s
   IF Char-t = "I"
      IF Next-t = "V"
      OR Next-t = "X"
      OR Next-t = "L"
      OR Next-t = "C"
      OR Next-t = "D"
      OR Next-t = "M"
         RESULT - 1
      ELSE
         RESULT + 1
      END IF

   // 5s
   ELSE IF Char-t = "V"
      IF Next-t = "X"
      OR Next-t = "L"
      OR Next-t = "C"
      OR Next-t = "D"
      OR Next-t = "M"
         RESULT - 5
      ELSE
         RESULT + 5
      END IF

   // 10s
   ELSE IF Char-t = "X"
      IF Next-t = "L"
      OR Next-t = "C"
      OR Next-t = "D"
      OR Next-t = "M"
         RESULT - 10
      ELSE
         RESULT + 10
      END IF

   // 50s
   ELSE IF Char-t = "L"
      IF Next-t = "C"
      OR Next-t = "D"
      OR Next-t = "M"
         RESULT - 50
      ELSE
         RESULT + 50
      END IF

   // 100s
   ELSE IF Char-t = "C"
      IF Next-t = "D"
      OR Next-t = "M"
         RESULT - 100
      ELSE
         RESULT + 100
      END IF

   // 500s
   ELSE IF Char-t = "D"
      IF Next-t = "M"
         RESULT - 500
      ELSE
         RESULT + 500
      END IF

   // 1000s
   ELSE IF Char-t = "M"
         RESULT + 1000
   END IF

   SET LoopLimit-n TO UNANSWERED
END REPEAT
SET Temp-n TO UNANSWERED
SET Char-t TO UNANSWERED
SET Next-t TO UNANSWERED

• Explanation •

This computation is for the rare occasion when you need to convert a Roman numeral back into a standard numeral (e.g. "MCMXCIX" > 1999). The result is a HotDocs number value.

NOTE: To format a standard numeral as a Roman numeral, just use the "IX" format for the number.

This computation uses a simple loop in the form of a repeated dialog called Loop. A detailed explanation of how the loop works is available in Computation #0015: Loops Via REPEAT. The dialog has a single number variable in it called LoopLimit-n. Set this variable's Advanced options to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file." The computation also uses two other temporary text variables, Char-t and Next-t. Set their Advanced options just like you did for LoopLimit-n. These variables store the current characters being analyzed.

The computation uses the Loop to examine each character of the Roman numeral, one at a time. On any given pass, the computation will store the current character in Char-t and the character following it (if any) in Next-t. We do this because we must see two Roman numerals at a time, as you'll see.

The computation then examines the character in Char-t to determine what it is. It must then look at the following character to see if it is higher in order. If so, the current character is reducing the overall value. Otherwise it is increasing it. For example, the character "I" normally increases the overall value by 1 (e.g. VI = 5 + 1, or 6; VIII = 5 + 1 + 1 + 1, or 8). But if a higher-order character follows it, it reduces the overall value by 1 (e.g. IV = -1 + 5, or 4; IX = -1 + 10, or 9).

The computation returns the numeric equivalent of the Roman numeral

 

• Model Template •

This template has everything you need set up and configured for you. It will work as-is, or can be adapted to your variable and dialog names. It contains: 1) sample Word and WordPerfect templates (or an Automator form) to demonstrate an implementation of the computation, 2) a component file containing the computation and all supporting dialogs and variables, and 3) instructions for adapting the computation for your use.

(Go to the download page)

 
 

• Contributors •

LegalCS