Created
December 27, 2019 08:55
-
-
Save saecmca/6f1ac02a855dcb9c358f5402c0e106da to your computer and use it in GitHub Desktop.
Number to words Romania
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mani; | |
import java.util.List; | |
//Mani @saecmca | |
public class TextToNum { | |
private static String input; | |
private static int num; | |
private static String[] units = | |
{"", | |
" unu", | |
" doi", | |
" trei", | |
" patru", | |
" cinci", | |
" șase", | |
" șapte", | |
" opt", | |
" nouă" | |
}; | |
private static String[] teen = | |
{" zece", | |
" unsprezece", | |
" doisprezece", | |
" treisprezece", | |
" paisprezece", | |
" paisprezece", | |
" șaisprezece", | |
" șaptesprezece", | |
" optsprezece", | |
" nouăsprezece" | |
}; | |
private static String[] tens = | |
{" douăzeci", | |
" Treizeci", | |
" Patruzeci", | |
" Cincizeci", | |
" Şaizeci", | |
" Șaptezeci", | |
" Optzeci", | |
" Nouăzeci" | |
}; | |
private static String[] maxs = | |
{"", | |
"", | |
" Sută", | |
" Mie", | |
" Lakh", | |
" Crore" | |
}; | |
public String convertNumberToWordsRom(double n) { | |
input = numToString(n); | |
String converted = ""; | |
int pos = 1; | |
boolean hun = false; | |
try { | |
while (input.length() > 0) { | |
if (pos == 1) // TENS AND UNIT POSITION | |
{ | |
if (input.length() >= 2) // TWO DIGIT NUMBERS | |
{ | |
String temp = input.substring(input.length() - 2, input.length()); | |
input = input.substring(0, input.length() - 2); | |
converted += digits(temp); | |
} else if (input.length() == 1) // 1 DIGIT NUMBER | |
{ | |
converted += digits(input); | |
input = ""; | |
} | |
pos++; | |
} else if (pos == 2) // HUNDRED POSITION | |
{ | |
String temp = input.substring(input.length() - 1, input.length()); | |
input = input.substring(0, input.length() - 1); | |
if (converted.length() > 0 && digits(temp) != "") { | |
converted = (digits(temp) + maxs[pos] + " si") + converted; | |
hun = true; | |
} else { | |
if | |
(digits(temp) == "") ; | |
else | |
converted = (digits(temp) + maxs[pos]) + converted; | |
hun = true; | |
} | |
pos++; | |
} else if (pos > 2) // REMAINING NUMBERS PAIRED BY TWO | |
{ | |
if (input.length() >= 2) // EXTRACT 2 DIGITS | |
{ | |
String temp = input.substring(input.length() - 2, input.length()); | |
input = input.substring(0, input.length() - 2); | |
if (!hun && converted.length() > 0) | |
converted = digits(temp) + maxs[pos] + " si" + converted; | |
else { | |
if (digits(temp) == "") ; | |
else | |
converted = digits(temp) + maxs[pos] + converted; | |
} | |
} else if (input.length() == 1) // EXTRACT 1 DIGIT | |
{ | |
if (!hun && converted.length() > 0) | |
converted = digits(input) + maxs[pos] + " si" + converted; | |
else { | |
if (digits(input) == "") ; | |
else | |
converted = digits(input) + maxs[pos] + converted; | |
input = ""; | |
} | |
} | |
pos++; | |
} | |
} | |
} catch (Exception e) { | |
Commons.printException("" + e); | |
} | |
return converted; | |
} | |
private String digits(String temp) // TO RETURN SELECTED NUMBERS IN WORDS | |
{ | |
String converted = ""; | |
try { | |
for (int i = temp.length() - 1; i >= 0; i--) { | |
int ch = temp.charAt(i) - 48; | |
if (i == 0 && ch > 1 && temp.length() > 1) | |
converted = tens[ch - 2] + converted; // IF TENS DIGIT STARTS WITH 2 OR MORE IT FALLS UNDER TENS | |
else if (i == 0 && ch == 1 && temp.length() == 2) // IF TENS DIGIT STARTS WITH 1 IT FALLS UNDER TEENS | |
{ | |
int sum = 0; | |
for (int j = 0; j < 2; j++) | |
sum = (sum * 10) + (temp.charAt(j) - 48); | |
return teen[sum - 10]; | |
} else { | |
if (ch > 0) | |
converted = units[ch] + converted; | |
} // IF SINGLE DIGIT PROVIDED | |
} | |
} catch (Exception e) { | |
Commons.printException("" + e); | |
} | |
return converted; | |
} | |
private String numToString(double x) // CONVERT THE NUMBER TO STRING | |
{ | |
String num = ""; | |
while (x != 0) { | |
num = ((char) ((x % 10) + 48)) + num; | |
x /= 10; | |
} | |
return num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment