@Test public void testRoman2Decimal() { System.out.println("Roman2Decimal"); assertEquals("1", Numek.Roman2Decimal("i")); assertEquals(null, Numek.Roman2Decimal("VL")); assertEquals("45", Numek.Roman2Decimal("XLV")); assertEquals(null, Numek.Roman2Decimal("IIII")); assertEquals("4", Numek.Roman2Decimal("iv")); assertEquals(null, Numek.Roman2Decimal("VIV")); assertEquals("9", Numek.Roman2Decimal("IX")); assertEquals(null, Numek.Roman2Decimal("CMM")); assertEquals("1900", Numek.Roman2Decimal("MCM")); assertEquals(null, Numek.Roman2Decimal("IXVI")); assertEquals("15", Numek.Roman2Decimal("XV")); assertEquals(null, Numek.Roman2Decimal("IVI")); assertEquals("5", Numek.Roman2Decimal("v")); assertEquals(null, Numek.Roman2Decimal("XXL")); assertEquals("30", Numek.Roman2Decimal("XXX")); assertEquals(null, Numek.Roman2Decimal("IC")); assertEquals("99", Numek.Roman2Decimal("XCIX")); assertEquals(null, Numek.Roman2Decimal("XIL")); assertEquals("41", Numek.Roman2Decimal("XLI")); assertEquals(null, Numek.Roman2Decimal("IXL")); assertEquals("39", Numek.Roman2Decimal("XXXIX")); }
public static String Roman2Decimal(String roman) { try { int dec = 0; int ant = 0; char ant_letter = '\0'; if (roman == null || roman.trim().length() == 0) { return "0"; } roman = roman.trim().replaceAll("\\s+", "").toUpperCase(); if (!roman.matches("[" + romans + "]+") || roman.matches(".*(.)\\1{3,}.*") || roman.matches(".*([" + romans5 + "]).*\\1.*")) { throw new Exception("Invalid roman number: " + roman + ". Must only contain " + romans + " and not more than 3 consecutive equal chars are allowed, non-10 power numbers (" + romans5 + ") can only appear once. " + roman); } for (int i = 0; i < roman.length(); i++) { char letter = roman.charAt(i); int value = romansMap.get("" + letter); dec = dec + value; if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant <= value && value < romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Two consecutive subtractions or more than one equal symbols used to subtract " + roman); } if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant < value && ant <= romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Substracting and adding the same symbol or greater " + roman); } if (i != 0 && ant < value) { double check5 = Math.log10(ant); if (i != 0 && check5 != (int) check5) { throw new Exception("Symbols powers of 5 cannot be used to substract: " + ant); } if (romans.indexOf(letter) - 2 > romans.indexOf(ant_letter)) { throw new Exception("With " + ant_letter + " you can only substract " + romans.substring(romans.indexOf(ant_letter) + 1, romans.indexOf(ant_letter) + 3) + ". Incorrect: " + roman); } dec = dec - ant * 2; } ant = value; ant_letter = letter; } return "" + dec; } catch (Exception e) { System.err.println("Errors found (NUMEK):\n\t" + e.toString()); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } return null; } }
Numek { public static String Roman2Decimal(String roman) { try { int dec = 0; int ant = 0; char ant_letter = '\0'; if (roman == null || roman.trim().length() == 0) { return "0"; } roman = roman.trim().replaceAll("\\s+", "").toUpperCase(); if (!roman.matches("[" + romans + "]+") || roman.matches(".*(.)\\1{3,}.*") || roman.matches(".*([" + romans5 + "]).*\\1.*")) { throw new Exception("Invalid roman number: " + roman + ". Must only contain " + romans + " and not more than 3 consecutive equal chars are allowed, non-10 power numbers (" + romans5 + ") can only appear once. " + roman); } for (int i = 0; i < roman.length(); i++) { char letter = roman.charAt(i); int value = romansMap.get("" + letter); dec = dec + value; if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant <= value && value < romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Two consecutive subtractions or more than one equal symbols used to subtract " + roman); } if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant < value && ant <= romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Substracting and adding the same symbol or greater " + roman); } if (i != 0 && ant < value) { double check5 = Math.log10(ant); if (i != 0 && check5 != (int) check5) { throw new Exception("Symbols powers of 5 cannot be used to substract: " + ant); } if (romans.indexOf(letter) - 2 > romans.indexOf(ant_letter)) { throw new Exception("With " + ant_letter + " you can only substract " + romans.substring(romans.indexOf(ant_letter) + 1, romans.indexOf(ant_letter) + 3) + ". Incorrect: " + roman); } dec = dec - ant * 2; } ant = value; ant_letter = letter; } return "" + dec; } catch (Exception e) { System.err.println("Errors found (NUMEK):\n\t" + e.toString()); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } return null; } } }
Numek { public static String Roman2Decimal(String roman) { try { int dec = 0; int ant = 0; char ant_letter = '\0'; if (roman == null || roman.trim().length() == 0) { return "0"; } roman = roman.trim().replaceAll("\\s+", "").toUpperCase(); if (!roman.matches("[" + romans + "]+") || roman.matches(".*(.)\\1{3,}.*") || roman.matches(".*([" + romans5 + "]).*\\1.*")) { throw new Exception("Invalid roman number: " + roman + ". Must only contain " + romans + " and not more than 3 consecutive equal chars are allowed, non-10 power numbers (" + romans5 + ") can only appear once. " + roman); } for (int i = 0; i < roman.length(); i++) { char letter = roman.charAt(i); int value = romansMap.get("" + letter); dec = dec + value; if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant <= value && value < romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Two consecutive subtractions or more than one equal symbols used to subtract " + roman); } if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant < value && ant <= romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Substracting and adding the same symbol or greater " + roman); } if (i != 0 && ant < value) { double check5 = Math.log10(ant); if (i != 0 && check5 != (int) check5) { throw new Exception("Symbols powers of 5 cannot be used to substract: " + ant); } if (romans.indexOf(letter) - 2 > romans.indexOf(ant_letter)) { throw new Exception("With " + ant_letter + " you can only substract " + romans.substring(romans.indexOf(ant_letter) + 1, romans.indexOf(ant_letter) + 3) + ". Incorrect: " + roman); } dec = dec - ant * 2; } ant = value; ant_letter = letter; } return "" + dec; } catch (Exception e) { System.err.println("Errors found (NUMEK):\n\t" + e.toString()); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } return null; } } Numek();  Numek(Locale l);  Numek(Locale l, String resources_dir); }
Numek { public static String Roman2Decimal(String roman) { try { int dec = 0; int ant = 0; char ant_letter = '\0'; if (roman == null || roman.trim().length() == 0) { return "0"; } roman = roman.trim().replaceAll("\\s+", "").toUpperCase(); if (!roman.matches("[" + romans + "]+") || roman.matches(".*(.)\\1{3,}.*") || roman.matches(".*([" + romans5 + "]).*\\1.*")) { throw new Exception("Invalid roman number: " + roman + ". Must only contain " + romans + " and not more than 3 consecutive equal chars are allowed, non-10 power numbers (" + romans5 + ") can only appear once. " + roman); } for (int i = 0; i < roman.length(); i++) { char letter = roman.charAt(i); int value = romansMap.get("" + letter); dec = dec + value; if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant <= value && value < romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Two consecutive subtractions or more than one equal symbols used to subtract " + roman); } if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant < value && ant <= romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Substracting and adding the same symbol or greater " + roman); } if (i != 0 && ant < value) { double check5 = Math.log10(ant); if (i != 0 && check5 != (int) check5) { throw new Exception("Symbols powers of 5 cannot be used to substract: " + ant); } if (romans.indexOf(letter) - 2 > romans.indexOf(ant_letter)) { throw new Exception("With " + ant_letter + " you can only substract " + romans.substring(romans.indexOf(ant_letter) + 1, romans.indexOf(ant_letter) + 3) + ". Incorrect: " + roman); } dec = dec - ant * 2; } ant = value; ant_letter = letter; } return "" + dec; } catch (Exception e) { System.err.println("Errors found (NUMEK):\n\t" + e.toString()); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } return null; } } Numek();  Numek(Locale l);  Numek(Locale l, String resources_dir); static Double calc_and_sum_frac(String snumber); String text2number(String snumber); final String disambiguate(String pattern); String getNormTextandPattern(String text, String pattern); static int countOccurrencesOf(String source, char pattern); static String Roman2Decimal(String roman); }
Numek { public static String Roman2Decimal(String roman) { try { int dec = 0; int ant = 0; char ant_letter = '\0'; if (roman == null || roman.trim().length() == 0) { return "0"; } roman = roman.trim().replaceAll("\\s+", "").toUpperCase(); if (!roman.matches("[" + romans + "]+") || roman.matches(".*(.)\\1{3,}.*") || roman.matches(".*([" + romans5 + "]).*\\1.*")) { throw new Exception("Invalid roman number: " + roman + ". Must only contain " + romans + " and not more than 3 consecutive equal chars are allowed, non-10 power numbers (" + romans5 + ") can only appear once. " + roman); } for (int i = 0; i < roman.length(); i++) { char letter = roman.charAt(i); int value = romansMap.get("" + letter); dec = dec + value; if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant <= value && value < romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Two consecutive subtractions or more than one equal symbols used to subtract " + roman); } if (i > 0 && roman.length() > 2 && i < roman.length() - 1 && (ant < value && ant <= romansMap.get("" + roman.charAt(i + 1)))) { throw new Exception("Substracting and adding the same symbol or greater " + roman); } if (i != 0 && ant < value) { double check5 = Math.log10(ant); if (i != 0 && check5 != (int) check5) { throw new Exception("Symbols powers of 5 cannot be used to substract: " + ant); } if (romans.indexOf(letter) - 2 > romans.indexOf(ant_letter)) { throw new Exception("With " + ant_letter + " you can only substract " + romans.substring(romans.indexOf(ant_letter) + 1, romans.indexOf(ant_letter) + 3) + ". Incorrect: " + roman); } dec = dec - ant * 2; } ant = value; ant_letter = letter; } return "" + dec; } catch (Exception e) { System.err.println("Errors found (NUMEK):\n\t" + e.toString()); if (System.getProperty("DEBUG") != null && System.getProperty("DEBUG").equalsIgnoreCase("true")) { e.printStackTrace(System.err); } return null; } } Numek();  Numek(Locale l);  Numek(Locale l, String resources_dir); static Double calc_and_sum_frac(String snumber); String text2number(String snumber); final String disambiguate(String pattern); String getNormTextandPattern(String text, String pattern); static int countOccurrencesOf(String source, char pattern); static String Roman2Decimal(String roman); public Locale locale; public HashSet<String> all_keys; public HashSet<String> repeated_keys; public PhraselistFile ambiguous; public PhraselistFile delimiters; public PhraselistFile units; public PhraselistFile tens; public PhraselistFile irregular_tens; public PhraselistFile magnitudes; public PhraselistFile special_groups; public PhraselistFile restrictions; public PhraselistFile ordinal_units; public PhraselistFile ordinal_irregular_tens; public PhraselistFile ordinal_tens; public PhraselistFile ordinal_suffixes; public PhraselistFile decimal_point_separator; public PhraselistFile group_separators; final static Integer MAX_NUMBERS_ORDER; static String romans; static String romans5; final static HashMap<String, Integer> romansMap; }