/* * [Japanese.java] * * Summary: Display a number in Japanese 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-04-12 */ package com.mindprod.inwords; /** * Display a number in Japanese words. *

* e.g. * -12345 -> "\u30de\u30a4\u30ca\u30b9\u4e00\u4e07\u4e8c\u5343\u4e09\u767e\u56db\u5341\u4e94" * This is the scheme used in Japan. * See also com.mindprod.inwords.JapaneseInRomanAlphabet. *

* This program is 1. "Japanese counting by Kanji (Chinese * Characters)". * There can be another variations. * 2. "Japanese counting by Kana (Japanese phonogram)" * 3. "Japanese counting by Roman alphabet" *

* Japanese counting is based on "4-zeros" system, * unlike from western * "3-zeros" system. Probably Chinese and Korean are * same because they * are supposed to be origins. But I am not sure. *

* Japanese Kanji in this program is coded in Unicode, so if the * code * is same in other Chinese Character using language, it may be * able * to be displayed. To be checked by each native speaker. *

* referring to Roedy Green's AmericanEnglish.java * subscribed to Roedy Green for his InWords package *

* * @author Roedy Green, Canadian Mind Products * @version 1.0 1999-04-12 * @ * @since 1999-04-12 */ public final class Japanese implements ToWords { /* * Japanese: unit, ten-thousand, hundred-million, trillion, ten-quadrillion, * hundred-quintillion */ private static final int divisor = 10000; /** * undisplayed copyright notice * * @noinspection UnusedDeclaration */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Hiroki Akimoto, mailto:akky@bigfoot.com"; // mainasu private static final String MINUS = "\u30de\u30a4\u30ca\u30b9"; // Rei private static final String ZERO = "\u96f6"; // none, jyu, hyaku, sen private static final String[] groupName1 = { /* * We only need up to a hundred-quintillion, since a long is about 9 * 10 ^ 18 */ /* * Japanese: unit, ten-thousand, hundred-million, trillion, * ten-quadrillion, hundred-quintillion */ "", "\u5341", "\u767e", "\u5343" }; // none, man, oku, chou, kei private static final String[] groupName2 = { /* * We only need up to a hundred-quintillion, since a long is about 9 * 10 ^ 18 */ /* * Japanese: unit, ten-thousand, hundred-million, trillion, * ten-quadrillion, hundred-quintillion */ "", "\u4e07", "\u61b6", "\u5146", "\u4eac", "\u5793" }; // none, ichi, ni, san, shi, go, roku, shichi, hachi, kyu private static final String[] lowName = { /* zero is shown as "" since it is never used in combined forms */ /* 0 .. 10 */ "", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d", "\u5341" }; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new Japanese() ); } // end main /** * convert long integer into Japanese words. e.g. -12345 -> * "\u30de\u30a4\u30ca\u30b9\u4e00\u4e07\u4e8c\u5343\u4e09\u767e\u56db\u5341\u4e94" * 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 ) { 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 ); num = num / divisor; if ( remdr == 0 ) { continue; } String t = ""; if ( remdr >= 1000 ) { int thous = remdr / 1000; remdr = remdr % 1000; if ( thous != 1 ) { t += lowName[ thous ]; } t += groupName1[ 3 ]; } if ( remdr >= 100 ) { int hunds = remdr / 100; remdr = remdr % 100; if ( hunds != 1 ) { t += lowName[ hunds ]; } t += groupName1[ 2 ]; } if ( remdr >= 10 ) { int tens = remdr / 10; remdr = remdr % 10; if ( tens != 1 ) { t += lowName[ tens ]; } t += groupName1[ 1 ]; } if ( remdr != 0 ) { t += lowName[ remdr ]; } s = t + groupName2[ group ] + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + s; } return s; } // end inWords } // end Japanese