การแปลงตัวเลขสากลเป็นตัวเลขโรมันใน Java

หากพูดถึงตัวเลขโรมัน หลายๆคนอาจจะรู้อยู่แล้ว หรือหลายๆคนอาจจะจำไม่ได้ แต่หากพูดถึงตัวเลข "I" บนหน้าปัดนาฬิกา บางคนอาจจะนึกออก จริงๆแล้วตัวเลขโรมันมีตั้งแต่เลข 1 - 3999 เท่านั้นครับ เพื่อไม่ให้เสียเวลา เรามาดูตัวอย่างกันครับ

ตัวอย่างโคด 
package demo;  
  
import java.math.BigDecimal;
  
/** 
 * @author nopphanan7 
 *  ตัวเลขโรมันมีค่าตั้งแต่ 1 ถึง 3999 เท่านั้น
 */  
public final class RomanNumber {  
      
    private static final String[] ROMAN = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX","V", "IV", "I"};
    private static final int[] DIGIT = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  
    private String valueText;  
  
    // ···········Methods··············  
    public String getText(int amount) {  
        BigDecimal value = new BigDecimal(amount);  
        this.valueText = getRomanNumber(value);  
        return this.valueText;  
    }  
  
    public String getText(long amount) {  
        BigDecimal value = new BigDecimal(amount);  
        this.valueText = getRomanNumber(value);  
        return this.valueText;  
    }  
  
    public String getText(String amount) {
        BigDecimal value = new BigDecimal(amount.trim());  
        this.valueText = getRomanNumber(value);  
        return this.valueText;  
    }  
  
    public String getText(Number amount) {  
        BigDecimal value = new BigDecimal(String.valueOf(amount));  
        this.valueText = getRomanNumber(value);  
        return this.valueText;  
    }  
  
    private static String getRomanNumber(BigDecimal amount) { 
        String roman = "";
        if (amount.compareTo(BigDecimal.ONE) < 0) {
            return "Value of RomanNumeral must be positive.";
        }
        if (amount.compareTo(BigDecimal.valueOf(new Long("3999"))) > 0 ) {
            return "Value of RomanNumeral must be 3999 or less.";
        }
        int N = Integer.parseInt(amount.toString());
        for (int i = 0; i < DIGIT.length; i++) {
           while (N >= DIGIT[i]) {
              roman += ROMAN[i];
              N -= DIGIT[i];
           }
        }
        return roman;
    }  
  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
     System.out.println("value of 1 : " + new RomanNumber().getText(1));
     System.out.println("value of 2 : " + new RomanNumber().getText(2));
        System.out.println("value of 3 : " + new RomanNumber().getText(3));  
        System.out.println("value of 4 : " + new RomanNumber().getText(4));
        System.out.println("value of 5 : " + new RomanNumber().getText(5));
        System.out.println("value of 6 : " + new RomanNumber().getText(6));
        System.out.println("value of 7 : " + new RomanNumber().getText(7));  
        System.out.println("value of 8 : " + new RomanNumber().getText(8));
        System.out.println("value of 9 : " + new RomanNumber().getText(9));
        System.out.println("value of 10 : " + new RomanNumber().getText(10));
        System.out.println("value of 123 : " + new RomanNumber().getText("123"));  
        System.out.println("value of 3234 : " + new RomanNumber().getText("3234"));  
    }  
  
}
ผลลัพธ์ที่ได้คือ
value of 1 : I
value of 2 : II
value of 3 : III
value of 4 : IV
value of 5 : V
value of 6 : VI
value of 7 : VII
value of 8 : VIII
value of 9 : IX
value of 10 : X
value of 123 : CXXIII
value of 3234 : MMMCCXXXIV

About Nop

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment