/* * [Norwegian.java] * * Summary: Display a number in Norwegian 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 og now appears properly in 1021 ett tusen og tjueen. * now pluralise millions on up, similar to Esperanto. * e.g. to milliarder 2,000,000,000. * 1.3 1999-01-21 make all language specific String literals into constants. * 1.4 1999-02-11 millioner now million, correct plural definition. */ package com.mindprod.inwords; /** * Display a number in Norwegian words. *

* e.g. -12345 is displayed as minus tolv tusen tre hundre og førtifem *

* * @author Roedy Green, Canadian Mind Products * @version 1.4 1999-02-11 millioner now million, correct plural definition. * Special thanks go to Tarjei �vergaard tarjeio@microdesign.no * who provided all the information about Norwegian counting * used to write this program. * @noinspection UnusedDeclaration * @since 1999-01-12 */ public final class Norwegian implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; private static final String AND = "og"; /** * undisplayed copyright notice */ 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 PLURAL = "er"; private static final String ZERO = "null"; private static final String[] groupName = { /* We only need up to a trillion, since a long is about 9 * 10 ^ 18 */ /* * American: unit, hundred, thousand, million, billion, trillion, * quadrillion, quintillion */ "", "hundre", "tusen", "million", "milliard", "billion", "billiard", "trillion" }; private static final String[] lowName = { /* zero is shown as "" since it is never used in combined forms */ /* 0 .. 19 */ "", "en", "to", "tre", "fire", "fem", "seks", "sju", "\u00e5tte" /* åtte */, "ni", "ti", "elleve", "tolv", "tretten", "fjorten", "femten", "seksten", "sytten", "atten", "nitten" }; private static final String[] ONE = { "ett", "en" }; private static final String[] tys = { /* 0, 10, 20, 30 ... 90 */ "", "", "tjue", "tretti", "f\u00f8rti" /* førti */, "femti", "seksti", "sytti", "\u00e5tti" /* åtti */, "nitti" }; 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 }; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new Norwegian(), new DecimalDots() ); } // end main /** * convert long integer into Norwegian words. e.g. -12345 -> "minus tolv tusen tre hundre og f�rtifem". 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[ group ] ); num = num / divisor[ group ]; String t; if ( remdr == 0 ) { continue; } if ( remdr == 1 ) { if ( group == 1 || group == 2 ) { t = ONE[ 0 ];// prefix form for 100 and 1000 } else { t = ONE[ 1 ];// usual form } } else 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 plural = ( remdr > 1 ) && ( group > 2 );// only above 1000 // considered plural s = ( and ? AND + " " : "" ) + t + " " + groupName[ group ] + ( plural ? PLURAL + " " : " " ) + s; } // end for s = s.trim(); if ( negative ) { s = MINUS + " " + s; } return s; } // end toWords } // end Norwegian