赞
踩
题目:
题解:
- class Solution {
- String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
- String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
- String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
- String[] thousands = {"", "Thousand", "Million", "Billion"};
-
- public String numberToWords(int num) {
- if (num == 0) {
- return "Zero";
- }
- StringBuffer sb = new StringBuffer();
- for (int i = 3, unit = 1000000000; i >= 0; i--, unit /= 1000) {
- int curNum = num / unit;
- if (curNum != 0) {
- num -= curNum * unit;
- sb.append(toEnglish(curNum)).append(thousands[i]).append(" ");
- }
- }
- return sb.toString().trim();
- }
-
- public String toEnglish(int num) {
- StringBuffer curr = new StringBuffer();
- int hundred = num / 100;
- num %= 100;
- if (hundred != 0) {
- curr.append(singles[hundred]).append(" Hundred ");
- }
- int ten = num / 10;
- if (ten >= 2) {
- curr.append(tens[ten]).append(" ");
- num %= 10;
- }
- if (num > 0 && num < 10) {
- curr.append(singles[num]).append(" ");
- } else if (num >= 10) {
- curr.append(teens[num - 10]).append(" ");
- }
- return curr.toString();
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。