/* * [BritishEnglish.java] * * Summary: display a number in British Engish. * * 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 change definition of trillion and quadrillion * one hundred _and_ one. * 1.3 1999-01-16 trillion for 10ˆ18 * 1.4 1999-01-21 make all language specific String literals into constants. */ package com.mindprod.inwords; /** * display a number in British Engish. *

* com.mindprod.inwords.BritishEnglish.java - convert long integer into * English words e.g. * -12345 -> "minus twelve thousand forty-five". * This is not the scheme used in the USA and Canada. * See com.mindprod.inwords.AmericanEnglish for the North American scheme * that * differs in the meaning * of billion, trillion etc. *

* Special thanks go to Richard Hindle <ritchie@sundog.demon.co.uk> * for information on how British numbering works. *

* Why is this code written in an unusual way? see inwords.use * * @author Roedy Green, Canadian Mind Products * @version 1.4 1999-01-21 make all language specific String literals into constants. * @since 1999-01-12 */ public final class BritishEnglish implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; private static final String AND = "and"; /** * 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 = { /* lack of lead space is deliberate. */ /* * We only need up to a quintillion, since a long is about 9 * 10 ^ 18 */ /* American: unit, hundred, thousand, million, trillion, quintillion */ "", "hundred", "thousand", "million", "billion", "trillion" }; 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 = { /* * how many of this group is needed to form one of the succeeding group. */ /* American: unit, hundred, thousand, million, trillion, quintillion */ 100, 10, 1000, 1000000, 1000000, 1000000 }; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new BritishEnglish() ); } // end main /** * convert long integer into British English words. -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 to words * * @return words * @noinspection WeakerAccess */ 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 ]; 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 ); } boolean and = ( group == 0 ) && ( num != 0 ); boolean comma = ( group > 2 ) && ( s.length() != 0 ) && ( !s.startsWith( AND ) ); s = ( and ? AND + " " : "" ) + t + " " + groupName[ group ] + ( comma ? ", " : " " ) + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + " " + s; } return s; } // end inWords } // end BritishEnglish