/* * [AmericanEnglish.java] * * Summary: display a number in American English words. * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 1999-01-12 * 1.1 1999-01-13 more table driven * handles longs, not just int. * 1.2 1999-01-14 1900 -> "ninteen hundred" * 1.3 1999-01-16 fix 21199 twenty-one thousand one hundred ninety-nine was coming out twenty thousand eleven * hundred ninety-nine. * 1.4 1999-01-16 5900 is now fify-nine hundred, 6900 is six thousand nine hundred. * 1.5 1999-01-20 correct so that 9900 is done as nine thousand nine hundred. * 1.6 1999-01-21 make all language specific String literals into constants. */ package com.mindprod.inwords; /** * display a number in American English words. *

* e.g. * -12345 -> "minus twelve thousand forty-five" * This is the scheme used in the USA and Canada. * See com.mindprod.inwords.BritishEnglish for the British scheme * that differs in the meaning * of billion. * * @author Roedy Green, Canadian Mind Products * @version 1.6 1999-01-21 make all language specific String literals into constants. * Why is this code written the way it is? see inwords.use * @since 1999-01-12 */ public class AmericanEnglish implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; /** * undisplayed copyright notice * * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String MINUS = "minus"; private static final String ZERO = "zero"; private static final String[] groupName = { /* * We only need up to a quintillion, since a long is about 9 * 10 ^ 18 */ /* * American: unit, hundred, thousand, million, billion, trillion, * quadrillion, quintillion */ "", "hundred", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion" }; private static final String[] lowName = { /* zero is shown as "" since it is never used in combined forms */ /* 0 .. 19 */ "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; private static final String[] tys = { /* 0, 10, 20, 30 ... 90 */ "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static final int[] divisor = { /* * HowToProcess many of this group is needed to form one of the succeeding group. */ /* * American: unit, hundred, thousand, million, billion, trillion, * quadrillion, quintillion */ 100, 10, 1000, 1000, 1000, 1000, 1000, 1000 }; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new AmericanEnglish() ); } // end main /** * convert long integer into American English words. e.g. -12345 -> "minus twelve thousand forty-five" Handles * negative and positive integers on range -Long.MAX_VALUE .. Long.MAX_VALUE; It cannot handle Long.MIN_VALUE; * * @param num number to convert into words * * @return number in words. */ public String toWords( long num ) { if ( num == 0 ) { return ZERO; } boolean negative = ( num < 0 ); if ( negative ) { num = -num; } String s = ""; // Work least significant digit to most, right to left. // until high order part is all 0s. for ( int group = 0; num > 0; group++ ) { int remdr = ( int ) ( num % divisor[ group ] ); num = num / divisor[ group ]; // check for 1100 .. 1999, 2100..2999, ... 5200..5999 // but not 1000..1099, 2000..2099, ... // Special case written as fifty-nine hundred. // e.g. thousands digit is 1..5 and hundreds digit is 1..9 // Only when no further higher order. if ( group == 1/* doing hundreds */ && 1 <= num && num <= 5 ) { if ( remdr > 0 ) { remdr += num * 10; num = 0; } // end if } // end if if ( remdr == 0 ) { continue; } String t; if ( remdr < 20 ) { t = lowName[ remdr ]; } else if ( remdr < 100 ) { int units = remdr % 10; int tens = remdr / 10; t = tys[ tens ]; if ( units != 0 ) { t += "-" + lowName[ units ]; } } else { t = toWords( remdr ); } s = t + " " + groupName[ group ] + " " + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + " " + s; } return s; } // end inWords } // end AmericanEnglish