Last active
September 18, 2023 04:02
-
-
Save madan712/be79a395424bf773be87 to your computer and use it in GitHub Desktop.
Java program to convert numbers to words
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
import java.text.NumberFormat; | |
public class NumberToWordsConverter { | |
public static final String[] units = { "", "One", "Two", "Three", "Four", | |
"Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", | |
"Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", | |
"Eighteen", "Nineteen" }; | |
public static final String[] tens = { | |
"", // 0 | |
"", // 1 | |
"Twenty", // 2 | |
"Thirty", // 3 | |
"Forty", // 4 | |
"Fifty", // 5 | |
"Sixty", // 6 | |
"Seventy", // 7 | |
"Eighty", // 8 | |
"Ninety" // 9 | |
}; | |
public static String convert(final int n) { | |
if (n < 0) { | |
return "Minus " + convert(-n); | |
} | |
if (n < 20) { | |
return units[n]; | |
} | |
if (n < 100) { | |
return tens[n / 10] + ((n % 10 != 0) ? " " : "") + units[n % 10]; | |
} | |
if (n < 1000) { | |
return units[n / 100] + " Hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100); | |
} | |
if (n < 100000) { | |
return convert(n / 1000) + " Thousand" + ((n % 10000 != 0) ? " " : "") + convert(n % 1000); | |
} | |
if (n < 10000000) { | |
return convert(n / 100000) + " Lakh" + ((n % 100000 != 0) ? " " : "") + convert(n % 100000); | |
} | |
return convert(n / 10000000) + " Crore" + ((n % 10000000 != 0) ? " " : "") + convert(n % 10000000); | |
} | |
public static void main(final String[] args) { | |
int n; | |
n = 5; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 16; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 50; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 78; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 456; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 1000; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 99999; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 199099; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
n = 10005000; | |
System.out.println(NumberFormat.getInstance().format(n) + "='" + convert(n) + "'"); | |
} | |
} |
Thank you sir!
Check this one also: https://github.com/ashprog/number_to_words
Hi, Any one have number to word convert in Romanian Java
hi , all Best
Check this one out (by method): https://tinyurl.com/mc25ea8
How about hundred thousands?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I Write a Java Program That prompts the user to enter a password and displays it back in the console window. The displayed password should be the entered password halved with the full password total number of characters added at the end.
example1:
enter your password: abcd
your password is : ab4
example2:
enter your password: abcde
your password is :ab5
Note: always assume that the user will enter a password with at least two characters. Additionally, you don’t need to consider the cases if the password length is an odd number (e.g: if the password length is 5 the program should only display the first two characters along with the full length).