Class ConvertingNumbers
- java.lang.Object
-
- bitchanger.calculations.ConvertingNumbers
-
public class ConvertingNumbers extends Object
TheConvertingNumbersclass 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 StringFRACTIONAL_PRECISION_INDICATORIndicator for the cut fractional part in case of converting from decimal system into a system of any basestatic intMAX_BASEConstant for the highest possible base of 36 characters given through 0-9 and A-Zstatic intMIN_BASEConstant for the lowest possible base
-
Constructor Summary
Constructors Modifier Constructor Description privateConvertingNumbers()Do not let anyone instantiate this class
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static doublebaseToDecFractionalPart(int base, String fractionalPart, Queue<ConversionStep> calcSteps)Converts the submitted decimal place of a number of any base into the decimal system.private static doublebaseToDecIntPart(int base, String integerPart, Queue<? super HornersMethod> calcSteps)Converts the submitted numberintegerPartof any basebaseinto the decimal system.static StringbaseToDecString(int base, String value)Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.static StringbaseToDecString(int base, String value, char comma)Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.static StringbaseToDecString(int base, String value, char comma, Queue<ConversionStep> calcPath)Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.private static voidcheckValue(int base, String value)Checks if the submitted stringvaluerepresents a number to the required basebaseand throws an exception, if this is not given.private static StringcomplementOf(String binValue)private static StringconvertDecFractionalToBaseString(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 StringconvertDecIntegerToBaseString(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 StringdecToBase(int newBase, String decValue)Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.static StringdecToBase(int newBase, String decValue, char comma)Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.static StringdecToBase(int newBase, String decValue, char comma, int fractionalPrecision)Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.static StringdecToBase(int newBase, String decValue, char comma, int fractionalPrecision, Queue<ConversionStep> calcPath)Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.static StringdecToBaseBlocks(int newBase, String decValue, char comma, int blockSize)Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation and splits the number into blockt with given length.private static chardigitOfValue(int value)Calculates the char for a value.static StringdoubleToString(double d)private static StringgetFirstComma(String value)Checks if the submitted string firstly contains a German comma (,) or an English comma (.).private static StringBuilderinsertSpace(StringBuilder sb, int blockSize)Splits the StringBuffer sb into blocks with given lengthstatic booleanisValueToBase(int base, String value)Checks ifvaluerepresents a number to the required basebase.private static String[]separateByComma(String value)Separated the numbers before and after the comma / point of the submitted stringvalue, which represents a number of the basebaseand returns these separated strings without leading 0 in the decimal part.static StringsplitInBlocks(int base, String value)Splits the String value into blocks of the given length.static StringsplitInBlocks(String value, int blockSize)Splits the String value into blocks of the given length.private static StringtrimToNumberString(String value)Deletes the FRACTIONAL_PRECISION_INDICATOR and spaces and converts all letters to uppercase.private static intvalueOfDigit(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
-
-
Method Detail
-
isValueToBase
public static boolean isValueToBase(int base, String value) throws IllegalArgumentExceptionChecks ifvaluerepresents a number to the required basebase. 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 asStringbase- requested base ofvalue- only values between 2 and 36 are allowed- Returns:
true, ifvaluerepresents a number to the requested basebase, otherwisefalsewill be returned.- Throws:
IllegalArgumentException- ifbasisleaves the range of value [2, 36]
-
doubleToString
public static String doubleToString(double d)
-
baseToDecString
public static String baseToDecString(int base, String value) throws NullPointerException, NumberFormatException, IllegalArgumentException, NumberOverflowException
Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.- Parameters:
base- Specific base of the submitted valuevaluevalue- The numeric value, which should be converted as string representation- Returns:
- Value of the Number in decimal system as string representation with default set comma
- Throws:
NullPointerException- if the parametervalueisnullNumberFormatException- if the parametervalueis not a number of basebaseIllegalArgumentException- ifvalueis an empty string orbasisleaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE
-
baseToDecString
public static String baseToDecString(int base, String value, char comma) throws NullPointerException, NumberFormatException, IllegalArgumentException, NumberOverflowException
Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.- Parameters:
base- Specific base of the submitted valuevaluevalue- The numeric value, which should be converted as string representationcomma- The char which is used as comma for floating point numbers- Returns:
- Value of the Number in decimal system as string representation
- Throws:
NullPointerException- if the parametervalueisnullNumberFormatException- if the parametervalueis not a number of basebaseIllegalArgumentException- ifvalueis an empty string orbasisleaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- if integer part ofvalueis greater or less +/-Long.MAX_VALUE
-
baseToDecString
public static String baseToDecString(int base, String value, char comma, Queue<ConversionStep> calcPath) throws NullPointerException, NumberFormatException, IllegalArgumentException, NumberOverflowException
Converts the submitted numbervalueof any basebaseinto a number of the decimal system as string representation.- Parameters:
base- Specific base of the submitted valuevaluevalue- The numeric value, which should be converted as string representationcomma- The char which is used as comma for floating point numberscalcPath- Queue to store every calculation step,nullis valid- Returns:
- Value of the Number in decimal system as string representation
- Throws:
NullPointerException- if the parametervalueisnullNumberFormatException- if the parametervalueis not a number of basebaseIllegalArgumentException- ifvalueis an empty string orbasisleaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- if integer part ofvalueis greater or less +/-Long.MAX_VALUE
-
decToBase
public static String decToBase(int newBase, String decValue) throws NullPointerException, NumberFormatException, NumberOverflowException
Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.If the indicator of decimal places is activated in the class
Preferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral system, which will the numberdecValuebe converted indecValue- Value of the number in decimal system as string representation- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException- If the parameterdecValueisnullNumberFormatException- If the parameterdecValueis not a number of base 10IllegalArgumentException- IfdecValueis an empty string ornewBaseleaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE- See Also:
Preferences
-
decToBase
public static String decToBase(int newBase, String decValue, char comma) throws NullPointerException, NumberFormatException, IllegalArgumentException, NumberOverflowException
Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.If the indicator of decimal places is activated in the class
Preferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral system, which will the numberdecValuebe converted indecValue- Value of the number in decimal system as string representationcomma- the char which is used as comma for floating point numbers- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException- If the parameterdecValueisnullNumberFormatException- If the parameterdecValueis not a number of base 10IllegalArgumentException- IfdecValueis an empty string ornewBaseleaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE- See Also:
Preferences
-
decToBase
public static String decToBase(int newBase, String decValue, char comma, int fractionalPrecision) throws NullPointerException, NumberFormatException, IllegalArgumentException, UnsupportedOperationException, NumberOverflowException
Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.If the indicator of decimal places is activated in the class
Preferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral system, which will the numberdecValuebe converted indecValue- Value of the number in decimal system as String representationcomma- the char which is used as comma for floating point numbersfractionalPrecision- maximum number of decimal places- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException- If the parameterdecValueisnullNumberFormatException- If the parameterdecValueis not a number of base 10IllegalArgumentException- IfdecValueis an empty String ornewBaseleaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)UnsupportedOperationException- IfdecValueis a negative value with decimal places andnewBasehas the value twoNumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE- See Also:
Preferences
-
decToBase
public static String decToBase(int newBase, String decValue, char comma, int fractionalPrecision, Queue<ConversionStep> calcPath) throws NullPointerException, NumberFormatException, IllegalArgumentException, UnsupportedOperationException, NumberOverflowException
Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation.If the indicator of decimal places is activated in the class
Preferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral system, which will the numberdecValuebe converted indecValue- Value of the number in decimal system as String representationcomma- the char which is used as comma for floating point numbersfractionalPrecision- maximum number of decimal placescalcPath- Queue to store every calculation step,nullis valid- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException- If the parameterdecValueisnullNumberFormatException- If the parameterdecValueis not a number of base 10IllegalArgumentException- IfdecValueis an empty String ornewBaseleaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)UnsupportedOperationException- IfdecValueis a negative value with decimal places andnewBasehas the value twoNumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE- See Also:
Preferences
-
decToBaseBlocks
public static String decToBaseBlocks(int newBase, String decValue, char comma, int blockSize) throws NullPointerException, NumberFormatException, IllegalArgumentException, NumberOverflowException
Converts the submitted numberdecValueof base 10 into a number of any basenewBaseas string representation and splits the number into blockt with given length.If the indicator of decimal places is activated in the class
Preferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral system, which will the numberdecValuebe converted indecValue- Value of the number in decimal system as string representationcomma- the char which is used as comma for floating point numbersblockSize- length of the blocks into which the string is splitted- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException- If the parameterdecValueisnullNumberFormatException- If the parameterdecValueis not a number of base 10IllegalArgumentException- IfdecValueis an empty string ornewBaseleaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)NumberOverflowException- ifvalueis greater or less +/-Long.MAX_VALUE- Since:
- Bitchanger 0.1.4
- See Also:
Preferences,decToBase(int, String, char),splitInBlocks(String, int)
-
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 splittedblockSize- 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 ofvaluevalue- String that is splitted- Returns:
- splitted String with blocks
- Since:
- Bitchanger 0.1.8
-
checkValue
private static void checkValue(int base, String value) throws NullPointerException, NumberFormatException, IllegalArgumentExceptionChecks if the submitted stringvaluerepresents a number to the required basebaseand throws an exception, if this is not given.- Parameters:
base- Base which isvaluechecked forvalue- String which is proofed- Throws:
NullPointerException- IfvalueisnullNumberFormatException- If the parametervalueis not a number of basebaseIllegalArgumentException- Ifbasisleaves the range of value [2, 36] orvalueis an empty string
-
baseToDecIntPart
private static double baseToDecIntPart(int base, String integerPart, Queue<? super HornersMethod> calcSteps)Converts the submitted numberintegerPartof any basebaseinto 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 ofintegerPartin the range [2; 36]integerPart- Number which will be converted as string representationcalcSteps- Queue to store every calculation step,nullis valid- Returns:
- Value of the submitted number as decimal representation as
double
-
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 offractionalPartin the range [2; 36]fractionalPart- Decimal places of a number which will be converted without comma as an integer as string representationcalcSteps- Queue to store every calculation step,nullis valid- Returns:
- Value of the submitted decimal place as decimal representation as
double
-
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 systemintegerPart- Integer value of decimal systemcalcSteps- Queue to store every calculation step,nullis valid- Returns:
- Returned value of base
basisas 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 classPreferencesthe cut decimal places caused due the maximum number of decimal places will be shown as "..."- Parameters:
newBase- Base of the new numeral systemfractionalPart- Decimal places of the decimal systemfractionalPrecision- Maximum number of decimal places in the resultcomma- Character that is placed as comma before the part of decimal placescalcSteps- Queue to store every calculation step,nullis 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 stringvalue, which represents a number of the basebaseand 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- Ifvalueis 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 splitblockSize- block's length- Returns:
- modified StringBuffer, that is same as the argument
- Since:
- Bitchanger 0.1.4
-
-