Class ConvertingNumbers


  • public class ConvertingNumbers
    extends Object
    The ConvertingNumbers class contains methods for performing conversions of numbers with different numeral systems.

    These methods can convert positive and negative integral and float-point numbers. Any numeral systems from base 2 to base 36 can be used. This exactly correspond to the character storage 0-9 and A-Z. As string representation, numbers with capital and lower case letters can be handed over to methods in this class. The return of these string only contains Numbers and upper case letters.

    The comma sign can be selected in German (,) or English (.) with the class Preferences. Furthermore the indicator for truncated fractional digits (...) can be activated or disabled.

    Since:
    Bitchanger 0.1.0
    Version:
    1.0.2
    Author:
    Tim Muehle, Moritz Wolter
    See Also:
    Preferences
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String FRACTIONAL_PRECISION_INDICATOR
      Indicator for the cut fractional part in case of converting from decimal system into a system of any base
      static int MAX_BASE
      Constant for the highest possible base of 36 characters given through 0-9 and A-Z
      static int MIN_BASE
      Constant for the lowest possible base
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ConvertingNumbers()
      Do not let anyone instantiate this class
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static double baseToDecFractionalPart​(int base, String fractionalPart, Queue<ConversionStep> calcSteps)
      Converts the submitted decimal place of a number of any base into the decimal system.
      private static double baseToDecIntPart​(int base, String integerPart, Queue<? super HornersMethod> calcSteps)
      Converts the submitted number integerPart of any base base into the decimal system.
      static String baseToDecString​(int base, String value)
      Converts the submitted number value of any base base into a number of the decimal system as string representation.
      static String baseToDecString​(int base, String value, char comma)
      Converts the submitted number value of any base base into a number of the decimal system as string representation.
      static String baseToDecString​(int base, String value, char comma, Queue<ConversionStep> calcPath)
      Converts the submitted number value of any base base into a number of the decimal system as string representation.
      private static void checkValue​(int base, String value)
      Checks if the submitted string value represents a number to the required base base and throws an exception, if this is not given.
      private static String complementOf​(String binValue)  
      private static String convertDecFractionalToBaseString​(int newBase, double fractionalPart, int fractionalPrecision, char comma, Queue<? super Multiplication> calcSteps)
      Converts decimal places of decimal system into a value of any base as string representation.
      private static String convertDecIntegerToBaseString​(int newBase, long integerPart, Queue<? super LongDivision> calcSteps)
      Converts an integer value of decimal system into a value of any base as string representation.
      static String decToBase​(int newBase, String decValue)
      Converts the submitted number decValue of base 10 into a number of any base newBase as string representation.
      static String decToBase​(int newBase, String decValue, char comma)
      Converts the submitted number decValue of base 10 into a number of any base newBase as string representation.
      static String decToBase​(int newBase, String decValue, char comma, int fractionalPrecision)
      Converts the submitted number decValue of base 10 into a number of any base newBase as string representation.
      static String decToBase​(int newBase, String decValue, char comma, int fractionalPrecision, Queue<ConversionStep> calcPath)
      Converts the submitted number decValue of base 10 into a number of any base newBase as string representation.
      static String decToBaseBlocks​(int newBase, String decValue, char comma, int blockSize)
      Converts the submitted number decValue of base 10 into a number of any base newBase as string representation and splits the number into blockt with given length.
      private static char digitOfValue​(int value)
      Calculates the char for a value.
      static String doubleToString​(double d)  
      private static String getFirstComma​(String value)
      Checks if the submitted string firstly contains a German comma (,) or an English comma (.).
      private static StringBuilder insertSpace​(StringBuilder sb, int blockSize)
      Splits the StringBuffer sb into blocks with given length
      static boolean isValueToBase​(int base, String value)
      Checks if value represents a number to the required base base.
      private static String[] separateByComma​(String value)
      Separated the numbers before and after the comma / point of the submitted string value, which represents a number of the base base and returns these separated strings without leading 0 in the decimal part.
      static String splitInBlocks​(int base, String value)
      Splits the String value into blocks of the given length.
      static String splitInBlocks​(String value, int blockSize)
      Splits the String value into blocks of the given length.
      private static String trimToNumberString​(String value)
      Deletes the FRACTIONAL_PRECISION_INDICATOR and spaces and converts all letters to uppercase.
      private static int valueOfDigit​(char digit)
      Calculates the place value of represented character.
    • Field Detail

      • MIN_BASE

        public static final int MIN_BASE
        Constant for the lowest possible base
        See Also:
        Constant Field Values
      • MAX_BASE

        public static final int MAX_BASE
        Constant for the highest possible base of 36 characters given through 0-9 and A-Z
        See Also:
        Constant Field Values
      • FRACTIONAL_PRECISION_INDICATOR

        public static final String FRACTIONAL_PRECISION_INDICATOR
        Indicator for the cut fractional part in case of converting from decimal system into a system of any base
        See Also:
        Constant Field Values
    • Constructor Detail

      • ConvertingNumbers

        private ConvertingNumbers()
        Do not let anyone instantiate this class
    • Method Detail

      • isValueToBase

        public static boolean isValueToBase​(int base,
                                            String value)
                                     throws IllegalArgumentException
        Checks if value represents a number to the required base base. If the indicator for truncated fractional digits is activated, it will be ignored in the evaluation. Whitespace can be used as thousands separator and will be ignored by this method.
        Parameters:
        value - number to be checked, represented as String
        base - requested base of value - only values between 2 and 36 are allowed
        Returns:
        true, if value represents a number to the requested base base , otherwise false will be returned.
        Throws:
        IllegalArgumentException - if basis leaves the range of value [2, 36]
      • doubleToString

        public static String doubleToString​(double d)
      • splitInBlocks

        public static String splitInBlocks​(String value,
                                           int blockSize)
        Splits the String value into blocks of the given length. This is used e.g. to separate thousands.
        Parameters:
        value - String that is splitted
        blockSize - block's length
        Returns:
        splitted String with blocks
        Since:
        Bitchanger 0.1.4
      • splitInBlocks

        public static String splitInBlocks​(int base,
                                           String value)
        Splits the String value into blocks of the given length. This is used e.g. to separate thousands.
        Parameters:
        base - base of value
        value - String that is splitted
        Returns:
        splitted String with blocks
        Since:
        Bitchanger 0.1.8
      • complementOf

        private static String complementOf​(String binValue)
      • baseToDecIntPart

        private static double baseToDecIntPart​(int base,
                                               String integerPart,
                                               Queue<? super HornersMethod> calcSteps)
        Converts the submitted number integerPart of any base base into the decimal system. For converting of any base into the decimal system, Horner-Schema will be used.
        Parameters:
        base - Any base of the numeral system of integerPart in the range [2; 36]
        integerPart - Number which will be converted as string representation
        calcSteps - Queue to store every calculation step, null is valid
        Returns:
        Value of the submitted number as decimal representation asdouble
      • baseToDecFractionalPart

        private static double baseToDecFractionalPart​(int base,
                                                      String fractionalPart,
                                                      Queue<ConversionStep> calcSteps)
        Converts the submitted decimal place of a number of any base into the decimal system. For converting of any base into the decimal system, Horner-Schema will be used.
        Parameters:
        base - Any base of the numeral system of fractionalPart in the range [2; 36]
        fractionalPart - Decimal places of a number which will be converted without comma as an integer as string representation
        calcSteps - Queue to store every calculation step, null is valid
        Returns:
        Value of the submitted decimal place as decimal representation asdouble
      • convertDecIntegerToBaseString

        private static String convertDecIntegerToBaseString​(int newBase,
                                                            long integerPart,
                                                            Queue<? super LongDivision> calcSteps)
        Converts an integer value of decimal system into a value of any base as string representation.
        Parameters:
        newBase - Base of the new numeral system
        integerPart - Integer value of decimal system
        calcSteps - Queue to store every calculation step, null is valid
        Returns:
        Returned value of base basis as string representation
        Since:
        Bitchanger 0.1.8
      • convertDecFractionalToBaseString

        private static String convertDecFractionalToBaseString​(int newBase,
                                                               double fractionalPart,
                                                               int fractionalPrecision,
                                                               char comma,
                                                               Queue<? super Multiplication> calcSteps)
        Converts decimal places of decimal system into a value of any base as string representation. If the indicator of decimal places is activated in the class Preferences the cut decimal places caused due the maximum number of decimal places will be shown as "..."
        Parameters:
        newBase - Base of the new numeral system
        fractionalPart - Decimal places of the decimal system
        fractionalPrecision - Maximum number of decimal places in the result
        comma - Character that is placed as comma before the part of decimal places
        calcSteps - Queue to store every calculation step, null is valid
        Returns:
        converted decimal places of the new base with leading comma as string representation
        Since:
        Bitchanger 0.1.8
        See Also:
        Preferences.setIndicateFractionalPrecision(boolean)
      • digitOfValue

        private static char digitOfValue​(int value)
        Calculates the char for a value. The characters 0 to 9 correspond to the values 0 to 9 - The letters A to Z correspond to the values 10 to 35.
        Parameters:
        value - Value of the position sought on decimal system
        Returns:
        Character that shows the place value of the submitted value
        See Also:
        valueOfDigit(char)
      • valueOfDigit

        private static int valueOfDigit​(char digit)
        Calculates the place value of represented character. The characters 0 to 9 correspond to the values 0 to 9 - The letters A to Z correspond to the values 10 to 35.
        Parameters:
        digit - Character whose value will be calculated
        Returns:
        Place value that is represented by digit
        See Also:
        digitOfValue(int)
      • separateByComma

        private static String[] separateByComma​(String value)
                                         throws NumberFormatException
        Separated the numbers before and after the comma / point of the submitted string value, which represents a number of the base base and returns these separated strings without leading 0 in the decimal part.
        Parameters:
        value - Number to be disassembled
        Returns:
        Integer in index 0 and decimal place in index 1, both as integer as string representation
        Throws:
        NumberFormatException - If value is not a number represented as string, caused due to many commas
      • getFirstComma

        private static String getFirstComma​(String value)
        Checks if the submitted string firstly contains a German comma (,) or an English comma (.).
        Parameters:
        value - String, which is checked if it contains a comma
        Returns:
        the comma which is on the front position or nullif it does not contain a comma
      • trimToNumberString

        private static String trimToNumberString​(String value)
        Deletes the FRACTIONAL_PRECISION_INDICATOR and spaces and converts all letters to uppercase. Returns the formatted String.
        Parameters:
        value - String to be formatted
        Returns:
        formatted String
      • insertSpace

        private static StringBuilder insertSpace​(StringBuilder sb,
                                                 int blockSize)
        Splits the StringBuffer sb into blocks with given length
        Parameters:
        sb - StringBuffer that gets split
        blockSize - block's length
        Returns:
        modified StringBuffer, that is same as the argument
        Since:
        Bitchanger 0.1.4