package com.wikiway.nebula.common.format; import java.util.Scanner; /** * 数字金额(元、分)转为英文 * * @see http://www.statman.info/conversions/number_scales.html * @author zzf 增强:1)增加小数金额 2)正确支持million、billion(原版不正确),金额限制<20亿(2 billion)。 */ public class NumToWords { String string; String st1[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", }; String st2[] = { "hundred", "thousand", "million", "billion" }; String st3[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", }; String st4[] = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; public String convert(double number) { return convert(number, "dollars", "cents"); } public String convert(double number, String dollarsUnit, String centsUnit) { int b1 = (int) number; String s1 = convert(b1, dollarsUnit); double b2 = number - b1; if (b2 != 0) { // 金额取两位小数 int cents = (int) Math.round(b2 * 100); // String s2=convert(cents,"cents"); // s1+=" and "+s2; s1 += " and " + cents + " " + cents; } else { s1 += " only"; } return s1; } private String convert(int number, String unit) { int n = 1; string = ""; while (number != 0) { number = convertSeg(number, n); n++; } string = string.trim().replaceAll(" ", " "); if (unit == null || unit.length() == 0) { return string; } return string + " " + unit; } private int convertSeg(int number, int n) { int word; switch (n) { case 1: word = number % 100; pass(word); if (number > 100 && number % 100 != 0) { show("and "); } number /= 100; break; case 2: word = number % 10; if (word != 0) { show(" "); show(st2[0]); show(" "); pass(word); } number /= 10; break; case 3: word = number % 1000; if (number < 1000) { show(" "); show(st2[1]); show(" "); int n1 = 1; while (number != 0) { number = convertSeg(number, n1); n1++; } } else if (word != 0) { show(" "); show(st2[1]); show(" "); pass(word); } number /= 1000; break; case 4: word = number % 1000; if (number < 1000) { show(" "); show(st2[2]); show(" "); int n2 = 1; while (number != 0) { number = convertSeg(number, n2); n2++; } } else if (word != 0) { show(" "); show(st2[2]); show(" "); pass(word); } number /= 1000; break; case 5: word = number % 100; if (word != 0) { show(" "); show(st2[3]); show(" "); pass(word); } number /= 100; break; } return number; } private void pass(int number) { int word, q; if (number < 10) { show(st1[number]); } else if (number < 20) { show(st3[number - 10]); } else if (number < 100) { word = number % 10; if (word == 0) { q = number / 10; show(st4[q - 2]); } else { q = number / 10; show(st1[word]); show(" "); show(st4[q - 2]); } } else { int n1 = 1; while (number != 0) { number = convertSeg(number, n1); n1++; } } } private void show(String s) { String st; st = string; string = s; string += st; } public static void main(String[] args) { NumToWords w = new NumToWords(); Scanner input = new Scanner(System.in); // 123450.6789 // should be:one hundred and twenty three thousand four hundred and // fifty dollars and 68 cents System.out.print("Enter Number: "); float num = input.nextFloat(); String inwords = w.convert(num); System.out.println(inwords); } }
最近下载更多