การแปลงค่าเงินบาทตัวเลขเป็นตัวหนังสืออังกฤษใน Java

จากบทความคราวที่แล้ว เรื่อง "การแปลงค่าเงินบาทตัวเลขเป็นตัวหนังสือไทย" และนี่คืออีกตัวอย่างหนึ่งครับ เป็นการแปลงค่าเงินตัวเลขบาทเป็นตัวหนังสืออังกฤษ.
package demo.currency;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @author nopphanan7
 * 
 */
public final class EngBaht {

    private static final String[] SCALE_BIG_EN = { "thousand", "million",
            "billion" };
    private static final String[] SCALE_TEN_EN = { "twenty", "thirty", "forty",
            "fifty", "sixty", "seventy", "eighty", "ninety" };
    private static final String[] DIGIT_EN = { "zero", "one", "two", "three",
            "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
            "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
            "seventeen", "eighteen", "nineteen" };
    private static final String[] SYMBOLS_TH = { "minus", "baht", "satang",
            "and", ",", " ", "฿" };

    private String valueText;

    // ···········Methods··············//
    public String getText(double amount) {
        BigDecimal value = new BigDecimal(amount);
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    public String getText(float amount) {
        BigDecimal value = new BigDecimal(amount);
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    public String getText(int amount) {
        BigDecimal value = new BigDecimal(amount);
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    public String getText(long amount) {
        BigDecimal value = new BigDecimal(amount);
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    public String getText(String amount) {
        // ไม่ต้องการเครื่องหมายคอมมาร์, ไม่ต้องการช่องว่าง,
        // ไม่ต้องการตัวหนังสือ บาท, ไม่ต้องการสัญลักษณ์สกุลเงินบาท
        for (String element : SYMBOLS_TH) {
            amount = amount.replace(element, "");
        }

        BigDecimal value = new BigDecimal(amount.trim());
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    public String getText(Number amount) {
        BigDecimal value = new BigDecimal(String.valueOf(amount));
        this.valueText = getThaiBaht(value);
        return this.valueText;
    }

    private static String getThaiBaht(BigDecimal amount) {
        StringBuilder builder = new StringBuilder();
        BigDecimal absolute = amount.abs();
        int precision = absolute.precision();
        int scale = absolute.scale();
        int rounded_precision = ((precision - scale) + 2);
        MathContext mc = new MathContext(rounded_precision,
                RoundingMode.HALF_UP);
        BigDecimal rounded = absolute.round(mc);
        BigDecimal[] compound = rounded.divideAndRemainder(BigDecimal.ONE);
        boolean negative_amount = (-1 == amount.compareTo(BigDecimal.ZERO));

        compound[0] = compound[0].setScale(0);
        compound[1] = compound[1].movePointRight(2);

        if (negative_amount) {
            builder.append(SYMBOLS_TH[0].toString() + " ");
        }

        builder.append(getNumberText(compound[0].toBigIntegerExact()));
        builder.append(SYMBOLS_TH[1].toString());

        if (0 == compound[1].compareTo(BigDecimal.ZERO)) {
            builder.append("");
        } else {
            builder.append(" " + SYMBOLS_TH[3].toString() + " ");
            builder.append(getNumberText(compound[1].toBigIntegerExact()));
            builder.append(" " + SYMBOLS_TH[2].toString());
        }

        return builder.toString();
    }

    private static String getNumberText(BigInteger number) {
        StringBuffer buffer = new StringBuffer();
        if (number.compareTo(new BigInteger("999")) <= 0) {
            return convertLessThanThousand(number.intValue());
        }
        int t = 0;
        while (number.compareTo(new BigInteger("0")) > 0) {
            if (number.intValue() % 1000 != 0) {
                StringBuffer buffer2 = new StringBuffer(
                        convertLessThanThousand(number.intValue() % 1000));
                if (t > 0) {
                    buffer2 = buffer2.append(" " + SCALE_BIG_EN[t - 1]);
                }
                if (buffer == null) {
                    buffer = buffer2;
                } else {
                    buffer = buffer2.append(" " + buffer);
                }
            }
            number = number.divide(new BigInteger("1000"));
            t++;
        }
        return buffer.toString();
    }

    private static String convertLessThanThousand(int number) {
        String digit_string = DIGIT_EN[number / 100] + " hundred";
        String convert_string = convertLessThanHundred(number % 100);
        if (number <= 99) {
            return convert_string;
        } else if (number % 100 == 0) {
            return digit_string;
        } else {
            return digit_string + " " + convert_string;
        }
    }

    private static String convertLessThanHundred(int number) {
        if (number < 20) {
            return DIGIT_EN[number];
        }
        String ten_string = SCALE_TEN_EN[number / 10 - 2];
        if (number % 10 == 0) {
            return ten_string;
        }
        return ten_string + "-" + DIGIT_EN[number % 10];
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Negative value of -1234.5463 : " + new EngBaht().getText(-1234.5463));
        System.out.println("Positive value of 1234.5463 : " + new EngBaht().getText(1234.5463));
        System.out.println("Negative string value of -1,234.5463 : " + new EngBaht().getText("-1,234.5463"));
        System.out.println("Positive string value of 1,234.5463 : " + new EngBaht().getText("1,234.5463"));
    }

}
ผลลัพธ์ที่ได้คือ
Negative value of -1234.5463 : minus one thousand two hundred thirty-four baht and fifty-five satang
Positive value of 1234.5463 : one thousand two hundred thirty-four baht and fifty-five satang
Negative string value of -1,234.5463 : minus one thousand two hundred thirty-four baht and fifty-five satang
Positive string value of 1,234.5463 : one thousand two hundred thirty-four baht and fifty-five satang

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