Skip to content

Commit

Permalink
Get english string representation of given number
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Cubelic committed Feb 12, 2018
0 parents commit a4e88ca
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>integerToStringConvertor</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dcubelic</groupId>
<artifactId>integerToStringConvertor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>Integer to String convertor</description>

</project>
86 changes: 86 additions & 0 deletions src/main/java/integerToStringConvertor/NumberUtility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package integerToStringConvertor;

/**
* Utility class used to convert integer (or long) numbers to their English representations.
* @author Dominik Čubelić
*
*/
public class NumberUtility {

private static String[] DIGITS = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
private static String[] TEN_THROUGH_NINETEEN = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};
private static String[] TENS = {"", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
private static String[] VALUES = {"", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion"};

private static String MINUS = "minus";
private static String HUNDRED = "hundred";
private static String ZERO = "zero";
private static String AND = "and";

/**
* Returns English string representation for a given number.
* @param number
* @return English string representation of given number.
*/
public static String getStringRepresentation(long number) {
StringBuilder sb = new StringBuilder();

boolean negative = number < 0;
number = Math.abs(number);

int br = 0;
while(number > 0) {
String tripleDigits = convertTripleDigits((int) (number%1000));
String str = tripleDigits.equals(ZERO) ? "" : tripleDigits + " " + VALUES[br] + " ";
number/=1000;
br++;
sb.insert(0, str);
}

if(negative) {
sb.insert(0, MINUS + " ");
}

return sb.toString().trim();
}

private static String convertSingleDigit(int digit) {
if(digit < 0 || digit > 9) {
throw new IllegalArgumentException("Invalid digit.");
}

return DIGITS[digit];
}

private static String convertDoubleDigits(int digits) {
if(digits < 0 || digits > 99) {
throw new IllegalArgumentException("Invalid digits");
}

if(digits < 10) return convertSingleDigit(digits);
else if (digits < 20) return TEN_THROUGH_NINETEEN[digits-10];

String tens = TENS[digits/10];
String digit = DIGITS[digits%10];

return tens + (digit.equals(ZERO) ? "" : " " + digit);
}

private static String convertTripleDigits(int digits) {
if(digits < 0 || digits > 999) {
throw new IllegalArgumentException("Invalid digits.");
}

if(digits < 100) return convertDoubleDigits(digits);

String firstDigitString = convertSingleDigit(digits/100);
int tens = digits%100;

String hundredsString = firstDigitString.equals(ZERO) ? "" : firstDigitString + " " + HUNDRED;
String tensString = convertDoubleDigits(tens);

return hundredsString + (tensString.equals(ZERO) ? "" : " " + AND + " " + tensString);
}

}
Binary file not shown.

0 comments on commit a4e88ca

Please sign in to comment.