patternjavaMinor
Decimal numbers to English string representation
Viewed 0 times
englishnumbersdecimalrepresentationstring
Problem
Is there anything that I could have reworked here? I have a Java 7 mind but I'm working on getting my Java 8 ready, so if there are any improvements over readability of performance here I'd love to read
```
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.regex.Pattern;
public final class EnglishNumberTranslator {
private static final double MAX_LIMIT = 999999.99;
private static final String OUT_OF_BOUNDS_INPUT = "Value must be greater than 0 "
+ "and lower or equal to " + String.valueOf(MAX_LIMIT);
private static final String INVALID_INPUT = "Unknow number pattern informed";
private static final String ZERO = "zero dollars";
private static final String[] oneToNineteenNames = {
"", // sentinel value
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
private static final String[] tenToNinetyNames = {
"", // sentinel value
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
/**
* Private constructor prevents initialization
* as this is only a method factory
*/
private EnglishNumberTranslator() {/deliberately empty/}
/**
* Calls the chain of methods that translate the value to a string
* representation of the decimal values.
* Dollars need to be separated by commas after three digits, such as:
* 12,345 for ten thousand three hundred and forty three,
* and cents must be informed as two digits after a dot, such as
* 1.45 for one and forty five
* @param value
```
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import java.util.regex.Pattern;
public final class EnglishNumberTranslator {
private static final double MAX_LIMIT = 999999.99;
private static final String OUT_OF_BOUNDS_INPUT = "Value must be greater than 0 "
+ "and lower or equal to " + String.valueOf(MAX_LIMIT);
private static final String INVALID_INPUT = "Unknow number pattern informed";
private static final String ZERO = "zero dollars";
private static final String[] oneToNineteenNames = {
"", // sentinel value
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
};
private static final String[] tenToNinetyNames = {
"", // sentinel value
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
/**
* Private constructor prevents initialization
* as this is only a method factory
*/
private EnglishNumberTranslator() {/deliberately empty/}
/**
* Calls the chain of methods that translate the value to a string
* representation of the decimal values.
* Dollars need to be separated by commas after three digits, such as:
* 12,345 for ten thousand three hundred and forty three,
* and cents must be informed as two digits after a dot, such as
* 1.45 for one and forty five
* @param value
Solution
I like it that your methods have easy-to-understand Javadocs, keep it up! :)
A few of your helper methods have the following signature:
This is possible a choice of style, but you can also consider returning the
Also, technically speaking, this is not a simple numbers-to-english translation, but an English representation of monetary values. You can alternatively call your class as
A few of your helper methods have the following signature:
private static boolean method() {
if (condition) {
return true;
}
return false;
}
private static String anotherMethod() {
if (condition) {
return string;
}
return anotherString;
}This is possible a choice of style, but you can also consider returning the
condition result directly, or using a ternary operator for the non-boolean return types. Using isValid(String) for example:private static boolean isValid(String number) {
return Pattern.matches("^\\d{1,3}(,\\d{3})*(\\.\\d\\d)?$", number);
}Also, technically speaking, this is not a simple numbers-to-english translation, but an English representation of monetary values. You can alternatively call your class as
MonetaryValueTranslator.Code Snippets
private static boolean method() {
if (condition) {
return true;
}
return false;
}
private static String anotherMethod() {
if (condition) {
return string;
}
return anotherString;
}private static boolean isValid(String number) {
return Pattern.matches("^\\d{1,3}(,\\d{3})*(\\.\\d\\d)?$", number);
}Context
StackExchange Code Review Q#119455, answer score: 2
Revisions (0)
No revisions yet.