/* * [Tagalog.java] * * Summary: display a number in Tagalog (Philipino) 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-17 */ package com.mindprod.inwords; /** * display a number in Tagalog (Philipino) words. *

* e.g. -12345 is displayed as minus labíndalawáng líbo tatlóng daán * apatnapú't limá *

* Based in information The Tuttle book: Basic Tagalog (ISBN: 0-8048-1910-6) * Thanks also to: Bernard Battitang <Bernard_Batitang@bc.sympatico.ca> * for information on large numbers and for proofing. * * @author Roedy Green, Canadian Mind Products * @version 1.0 1999-01-17 * @since 1999-01-17 */ public final class Tagalog implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; private static final String ALTHUNDRED = "ra\u00e1n"; private static final String ALTONE = "san"; /** * 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 = "nol"; private static final String[] digits = { /* names of digits 0..10 */ /* zero is shown as "" since is "nol" is not used in combinations */ "", /* 0 */ "is\u00e1" /* 1 isá isá */, "dalaw\u00e1" /* 2 dalawá dalawá */, "tatl\u00f3" /* 3 tatló tatló */, "\u00e1pat" /* 4 ápat ápat */, "lim\u00e1" /* 5 limá limá */, "\u00e1nim" /* 6 ánim ánim */, "pit\u00f3" /* 7 pitó pitó */, "wal\u00f3" /* 8 waló waló */, "siy\u00e1m" /* 9 siyám siyám */, "samp\u00fb" /* 10 samp� sampû */ }; private static final String[] groupName = { "", "da\u00e1n" /* 100 daán da´n */, "l\u00edbo" /* 1,000 líbo líbo */, "milyon" /* 1,000,000 */, "bilyon" /* 1,000,000,000 */, "trilyon" /* 1,000,000,000,000 */, "kwadrilyon" /* 1,000,000,000,000,000 */, "kwintilyon" /* 1,000,000,000,000,000,000 */ }; private static final String[] TYS = { "" /* 00 */, "samp\u00fb" /* 10 samp� sampû */, "dalawamp\u00fb" /* 20 dalawamp� dalawampû */, "tatlump\u00fb" /* 30 tatlump� tatlumpû */, "apatnap\u00fb" /* 40 apatnap� apatnapû */, "limamp\u00fb" /* 50 limamp� limampû */, "animnap\u00fb" /* 60 animnap� animnapû */, "pitump\u00fb" /* 70 pitump� pitumpû */, "walump\u00fb" /* 80 walump� walumpû */, "siyamnap\u00fb" /* 90 siyamnap� siyamnapû */ }; private static final int[] divisor = { /* * how 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 }; /** * Appends "and" to a number in words. e.g. dalawamp� -> dalawup�'t, daán -> daá't * * @param s String representing the number in words * * @return append the word "and" in Tagalog. */ private static String appendAnd( String s ) { switch ( s.charAt( s.length() - 1 ) ) { case '\u00fb'/* � , chop �, and add �'t */: return s.substring( 0, s.length() - 1 ) + "\u00fa't"/* �'t */; case 'o': case 'n': default: /* chop last letter, and add 't */ return s.substring( 0, s.length() - 1 ) + "'t"/* t */; } // end switch } // end appendAnd /** * Get number in words 1..19. * * @param i number 1 to 19. * * @return a small number in words. */ private static String lowName( int i ) { String s; if ( i < 11 ) { s = digits[ i ]; } else { s = teens( i ); } return s; } // end lowName /** * Takes standalone number in words, and makes it muliplicative so it can be used ahead of hundred, thousand etc. * * @param s plain number in words. * * @return number in words modified for use as multiplicative prefix. */ private static String makeMultiplicative( String s ) { // basically append "ng", but with variant depending on last char String suffix; switch ( s.charAt( s.length() - 1 ) ) { case 'm': case 't': suffix = " na"; break; default: suffix = "ng"; break; } // end switch return s + suffix; } /** * get number 11..19 in words, * * @param teen 11 .. 19. Returns corresponding word e.g. 17 -> labimpitó. generated from corresponding * digit 1..9. * * @return number in words in the 11 to 19. */ private static String teens( int teen ) { String prefix; switch ( teen ) { case 11: case 14: case 16: prefix = "lab\u00edng-"/* e.g.labíng-isá */; break; case 12: case 13: case 15: prefix = "lab\u00edn"/* e.g. labíndalawá */; break; case 17: prefix = "labim"/* e.g. labimpitó */; break; case 18: prefix = "labing"/* e.g. labingwaló */; break; case 19: prefix = "labin"/* e.g. labinsiyám */; break; default: throw new IllegalArgumentException( "teens parm not 11..19" ); } // end switch return prefix + digits[ teen % 10 ]; } // end teens /** * convert long integer into Tagalog words e.g. -12345 -> "minus labíndalawáng líbo * tatlóng daán apatnap�'t limá". * 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 * @param andDone true if already emitted the word "and" to the right. * * @return words */ private static String toWordsCore( long num, boolean andDone ) { 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; if ( units == 0 ) { t = TYS[ tens ]; } else { t = appendAnd( TYS[ tens ] ) + " " + lowName( units ); andDone = true; } } else { t = toWordsCore( remdr, andDone ); } String groupWord = groupName[ group ]; boolean leftPad = ( t.length() > 0 ); boolean rightPad = ( groupWord.length() > 0 ); // word for hundred changes from daán to raán if preceeded by 4??? if ( group == 1 && remdr % 10 == 4 ) { groupWord = ALTHUNDRED; } if ( groupWord.length() > 0 ) { t = makeMultiplicative( t ); } // word for 1 before 100 changes from isáng to san, and leftpad is // suppressed. if ( group == 1 && remdr == 1 ) { t = ALTONE; leftPad = false; } if ( !andDone && ( s.length() != 0 ) ) { groupWord = appendAnd( groupWord ); andDone = true; } s = t + ( leftPad ? " " : "" ) + groupWord + ( rightPad ? " " : "" ) + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + " " + s; } return s; } // end inWords /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new Tagalog() ); } // end main /** * convert long integer into Tagalog words e.g. -12345 -> "minus labíndalawáng líbo * tatlóng daán apatnap�'t limá". * 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 */ @SuppressWarnings( { "WeakerAccess" } ) public String toWords( long num ) { return toWordsCore( num, false ); } } // end Tagalog