Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created May 4, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/b6e6026f45a6328f8ab9de027d74e37c to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/b6e6026f45a6328f8ab9de027d74e37c to your computer and use it in GitHub Desktop.
package maricka.kolokolova;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("How much money do you have?");
double num = sc.nextDouble();
String str = String.valueOf(num);
sc.close();
String[] numbers = new String[91];
numbers[0] = "";
numbers[1] = "one";
numbers[2] = "two";
numbers[3] = "three";
numbers[4] = "four";
numbers[5] = "five";
numbers[6] = "six";
numbers[7] = "seven";
numbers[8] = "eight";
numbers[9] = "nine";
numbers[10] = "ten";
numbers[11] = "eleven";
numbers[12] = "twelve";
numbers[13] = "thirteen";
numbers[14] = "fourteen";
numbers[15] = "fiveteen";
numbers[16] = "sixteen";
numbers[17] = "seventeen";
numbers[18] = "eighteen";
numbers[19] = "nineteen";
numbers[20] = "twenty";
numbers[30] = "thirty";
numbers[40] = "forty";
numbers[50] = "fifty";
numbers[60] = "sixty";
numbers[70] = "seventy";
numbers[80] = "eighty";
numbers[90] = "ninety";
char d = '.';
int c;
String stringDollar;
String stringCent;
String strNew = "";
String prd = "";
String strFinalDollar = "";
String strFinalDollarTmp = "";
String strFinalCent = "";
int s = 0;
int n;
int nPr;
c = str.indexOf(d);
stringDollar = str.substring(0, c);
stringCent = str.substring(c + 1, str.length());
char[] dollar = stringDollar.toCharArray();
char[] cent = stringCent.toCharArray();
int st = 0;
int ed = 0;
// dollar
int step;
if (stringDollar.length() % 3 == 0) {
step = stringDollar.length() / 3;
} else {
step = stringDollar.length() / 3 + 1;
}
for (int i = 1; i <= step; i++) {
if (i == 1) {
ed = dollar.length - 1;
} else {
ed = st - 1;
}
st = dollar.length - 3 * i;
if (st < 0) {
st = 0;
}
s = 0;
strFinalDollarTmp = "";
for (int j = ed; j >= st; j--) {
s = s + 1;
n = Character.getNumericValue(dollar[j]);
strNew = numbers[n];
if (s == 2 && n == 1) {
nPr = Character.getNumericValue(dollar[j + 1]);
strFinalDollarTmp = "";
// 10,11,12...
strNew = numbers[nPr + 10];
}
if (s == 2 && n != 1) {
// 20,30,40...
strNew = numbers[n * 10];
}
if (s == 3) {
strNew = strNew + " hundred";
}
if (j == dollar.length - 4) {
prd = " thousand";
}
if (j == dollar.length - 7) {
prd = " million";
}
if (j == dollar.length - 10) {
prd = " billion";
}
strFinalDollarTmp = strNew + " " + prd + " " + strFinalDollarTmp;
prd = "";
}
strFinalDollar = strFinalDollarTmp + strFinalDollar;
}
// Cents
s = 0;
for (int k = 0; k < cent.length; k++) {
s = s + 1;
n = Character.getNumericValue(cent[k]);
if (s == 1) {
// 20,30,40...
strNew = numbers[n * 10];
} else {
nPr = Character.getNumericValue(cent[k - 1]);
if (s == 2 && nPr == 1) {
strFinalCent = "";
// 10,11,12...
strNew = numbers[n + 10];
} else {
strNew = numbers[n];
}
}
strFinalCent = strFinalCent + " " + strNew;
}
if (strFinalCent.contentEquals(" ")) {
strFinalCent = "";
} else {
strFinalCent = strFinalCent + " cents";
}
if (strFinalDollar.contentEquals(" ")) {
strFinalDollar = "";
} else {
strFinalDollar = strFinalDollar + " dollars";
}
if (!strFinalCent.isEmpty() && !strFinalDollar.isEmpty()) {
strFinalCent = " and " + strFinalCent;
}
System.out.println("You have: " + strFinalDollar + strFinalCent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment