Skip to content

Commit

Permalink
Added fromNumeral method
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jan 10, 2021
1 parent a34b36d commit db9c60c
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/willfp/eco/util/NumberUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.willfp.eco.util;

import com.sun.javaws.exceptions.InvalidArgumentException;
import lombok.experimental.UtilityClass;

import java.text.DecimalFormat;
Expand Down Expand Up @@ -85,6 +86,30 @@ public String toNumeral(final int number) {
}
}

/**
* Get number from roman numeral.
*
* @param numeral The numeral to convert.
* @return The number, converted from a roman numeral.
*/
public static int fromNumeral(String numeral) {
if (numeral.isEmpty()) return 0;
if (numeral.startsWith("M")) return 1000 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("CM")) return 900 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("D")) return 500 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("CD")) return 400 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("C")) return 100 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("XC")) return 90 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("L")) return 50 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("XL")) return 40 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("X")) return 10 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("IX")) return 9 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("V")) return 5 + fromNumeral(numeral.substring(1));
if (numeral.startsWith("IV")) return 4 + fromNumeral(numeral.substring(2));
if (numeral.startsWith("I")) return 1 + fromNumeral(numeral.substring(1));
throw new IllegalArgumentException("Number is invalid!");
}

/**
* Generate random integer in range.
*
Expand Down

0 comments on commit db9c60c

Please sign in to comment.