У меня есть код, правда он громоздкий:
Код | private static String sumInWord(double totalSum, int depth) { String[] orders = new String[]{"рубль","рубля","рублей","тысяча","тысячи","тысяч","миллион","миллиона","миллионов","миллиард","миллиарда","миллиардов"}; String[] valuesThousand = new String[]{"одна","две","три","четыре","пять","шесть","семь","восемь","девять","десять","одиннадцать","двенадцать","тринадцать","четырнадцать","пятнадцать","шестнадцать","семнадцать","восемнадцать","девятнадцать"}; String[] valuesMills = new String[]{"один","два","три","четыре","пять","шесть","семь","восемь","девять","десять","одиннадцать","двенадцать","тринадцать","четырнадцать","пятнадцать","шестнадцать","семнадцать","восемнадцать","девятнадцать"}; String[] valuesTens = new String[]{"десять","двадцать","тридцать","сорок","пятьдесят","шестьдесят","семьдесят","восемьдесят","девяносто"}; String[] valuesHundreds = new String[]{"сто","двести","триста","четыреста","пятьсот","шестьсот","семьсот","восемьсот","девятьсот"}; String s = "";
if ((int)(totalSum / 1000) > 0) { if (depth < 3) s = s.concat(sumInWord(totalSum / 1000, depth + 1)); else return "Слишком большое число"; } totalSum %= 1000;
//анализ сотен if ((int)(totalSum / 100) > 0) { s = s.concat(" "); s = s.concat(valuesHundreds[(int)(totalSum / 100) - 1]); } totalSum %= 100;
//анализ десятков if ((int)(totalSum / 10) > 0) { s = s.concat(" "); if ((int)(totalSum / 10) == 1) { s = s.concat(valuesMills[(int)totalSum - 1]); s = s.concat(" ").concat(orders[depth * 3 + 2]); } else { s = s.concat(valuesTens[(int)(totalSum / 10) - 1]); } } //анализ единиц if ((int)(totalSum / 10) != 1) {
if ((int)totalSum != 0) {
if ((int)totalSum % 10 != 0) { s = s.concat(" "); if (depth != 1) { s = s.concat(valuesMills[(int)(totalSum % 10) - 1]); } else { s = s.concat(valuesThousand[(int)(totalSum % 10) -1]); } } } else { if (depth == 0) s = s.concat("ноль целых"); }
if ((int)totalSum != 0) { s = s.concat(" "); switch ((int)(totalSum % 10)) { case 0: case 5: case 6: case 7: case 8: case 9:s = s.concat(orders[depth * 3 + 2]);break; case 1:s = s.concat(orders[depth * 3 + 0]);break; case 2: case 3: case 4:s = s.concat(orders[depth * 3 + 1]);break; } } } //анализ сотых if (depth == 0) { totalSum *= 100; totalSum %= 100; if (Math.floor(totalSum) != Math.floor(totalSum + 0.1)) totalSum = Math.floor(totalSum + 0.1); else totalSum = Math.floor(totalSum);
if ((int)(totalSum / 10) > 0) { s = s.concat(" "); if ((int)(totalSum / 10) == 1) { s = s.concat(valuesMills[(int)totalSum - 1]); s = s.concat(" копеек"); } else { s = s.concat(valuesTens[(int)(totalSum / 10) - 1]); } } if ((int)(totalSum / 10) != 1) {
if ((int)totalSum != 0 && (int)totalSum % 10 != 0) { s = s.concat(" "); s = s.concat(valuesThousand[(int)(totalSum % 10) - 1]); }
if ((int)totalSum != 0) { s = s.concat(" "); switch ((int)(totalSum % 10)) { case 2: case 3: case 4:s = s.concat("копейки");break; case 0: case 5: case 6: case 7: case 8: case 9:s = s.concat("копеек");break; case 1:s = s.concat("копейка");break; } } } } return s; }
|
Вызывать метод лучше так
Код | sumInWord((float) xxx, 0);
|
где ххх - ваше число.
Писал на скорую руку, так что если какие баги найдете или знаете как упростить пишите - буду признателен. |