/* * [Octal.java] * * Summary: Display a number in octal digits. * * 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 octal digits. *

* e.g. * -12345 -> 1_777_777_777_777_777_747_707 *

* * @author Roedy Green, Canadian Mind Products * @version 1.0 1999-01-17 * @ * @noinspection UnusedDeclaration * @since 1999-01-17 */ public final class Octal implements ToWords { private static final int FIRST_COPYRIGHT_YEAR = 1999; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; /** * test harness * * @param args not used */ public static void main( String[] args ) { Test.test( new Octal() ); } // end main /** * convert long integer into Octal e.g. -12345 -> 1_777_777_777_777_777_747_707 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 ) { String h; if ( num < 0 ) { h = Long.toString( num & 0x7fffffffffffffffL, 8 ); // pad out to 21 digits h = "000000000000000000000".substring( h.length() ) + h; h = Integer.toString( ( int ) ( num >>> 63 ), 8 ) + h; } else { h = Long.toString( num, 8 ); // pad out to 22 digits h = "0000000000000000000000".substring( h.length() ) + h; } // insert decorative underscores every 3 chars to help make it easier to // read. return h.substring( 0, 1 ) + '_' + h.substring( 1, 4 ) + '_' + h.substring( 4, 7 ) + '_' + h.substring( 7, 10 ) + '_' + h.substring( 10, 13 ) + '_' + h.substring( 13, 16 ) + '_' + h.substring( 16, 19 ) + '_' + h.substring( 19 ); } // end toWords } // end Octal