Class ConvertingNumbers
- java.lang.Object
-
- bitchanger.calculations.ConvertingNumbers
-
public class ConvertingNumbers extends Object
TheConvertingNumbers
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 basestatic int
MAX_BASE
Constant for the highest possible base of 36 characters given through 0-9 and A-Zstatic 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 numberintegerPart
of any basebase
into the decimal system.static String
baseToDecString(int base, String value)
Converts the submitted numbervalue
of any basebase
into a number of the decimal system as string representation.static String
baseToDecString(int base, String value, char comma)
Converts the submitted numbervalue
of any basebase
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 numbervalue
of any basebase
into a number of the decimal system as string representation.private static void
checkValue(int base, String value)
Checks if the submitted stringvalue
represents a number to the required basebase
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 numberdecValue
of base 10 into a number of any basenewBase
as string representation.static String
decToBase(int newBase, String decValue, char comma)
Converts the submitted numberdecValue
of base 10 into a number of any basenewBase
as string representation.static String
decToBase(int newBase, String decValue, char comma, int fractionalPrecision)
Converts the submitted numberdecValue
of base 10 into a number of any basenewBase
as string representation.static String
decToBase(int newBase, String decValue, char comma, int fractionalPrecision, Queue<ConversionStep> calcPath)
Converts the submitted numberdecValue
of base 10 into a number of any basenewBase
as string representation.static String
decToBaseBlocks(int newBase, String decValue, char comma, int blockSize)
Converts the submitted numberdecValue
of base 10 into a number of any basenewBase
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 lengthstatic boolean
isValueToBase(int base, String value)
Checks ifvalue
represents 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 basebase
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
-
-
Method Detail
-
isValueToBase
public static boolean isValueToBase(int base, String value) throws IllegalArgumentException
Checks ifvalue
represents 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 asString
base
- requested base ofvalue
- only values between 2 and 36 are allowed- Returns:
true
, ifvalue
represents a number to the requested basebase
, otherwisefalse
will be returned.- Throws:
IllegalArgumentException
- ifbasis
leaves 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 numbervalue
of any basebase
into a number of the decimal system as string representation.- Parameters:
base
- Specific base of the submitted valuevalue
value
- 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 parametervalue
isnull
NumberFormatException
- if the parametervalue
is not a number of basebase
IllegalArgumentException
- ifvalue
is an empty string orbasis
leaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- ifvalue
is 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 numbervalue
of any basebase
into a number of the decimal system as string representation.- Parameters:
base
- Specific base of the submitted valuevalue
value
- 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 parametervalue
isnull
NumberFormatException
- if the parametervalue
is not a number of basebase
IllegalArgumentException
- ifvalue
is an empty string orbasis
leaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- if integer part ofvalue
is 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 numbervalue
of any basebase
into a number of the decimal system as string representation.- Parameters:
base
- Specific base of the submitted valuevalue
value
- 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,null
is valid- Returns:
- Value of the Number in decimal system as string representation
- Throws:
NullPointerException
- if the parametervalue
isnull
NumberFormatException
- if the parametervalue
is not a number of basebase
IllegalArgumentException
- ifvalue
is an empty string orbasis
leaves the range of value [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- if integer part ofvalue
is greater or less +/-Long.MAX_VALUE
-
decToBase
public static String decToBase(int newBase, String decValue) throws NullPointerException, NumberFormatException, NumberOverflowException
Converts the submitted numberdecValue
of base 10 into a number of any basenewBase
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, which will the numberdecValue
be 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 parameterdecValue
isnull
NumberFormatException
- If the parameterdecValue
is not a number of base 10IllegalArgumentException
- IfdecValue
is an empty string ornewBase
leaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- ifvalue
is 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 numberdecValue
of base 10 into a number of any basenewBase
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, which will the numberdecValue
be 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 parameterdecValue
isnull
NumberFormatException
- If the parameterdecValue
is not a number of base 10IllegalArgumentException
- IfdecValue
is an empty string ornewBase
leaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- ifvalue
is 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 numberdecValue
of base 10 into a number of any basenewBase
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, which will the numberdecValue
be 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 parameterdecValue
isnull
NumberFormatException
- If the parameterdecValue
is not a number of base 10IllegalArgumentException
- IfdecValue
is an empty String ornewBase
leaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)
UnsupportedOperationException
- IfdecValue
is a negative value with decimal places andnewBase
has the value twoNumberOverflowException
- ifvalue
is 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 numberdecValue
of base 10 into a number of any basenewBase
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, which will the numberdecValue
be 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,null
is valid- Returns:
- Converted number of the submitted base as string representation with default set comma
- Throws:
NullPointerException
- If the parameterdecValue
isnull
NumberFormatException
- If the parameterdecValue
is not a number of base 10IllegalArgumentException
- IfdecValue
is an empty String ornewBase
leaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)
UnsupportedOperationException
- IfdecValue
is a negative value with decimal places andnewBase
has the value twoNumberOverflowException
- ifvalue
is 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 numberdecValue
of base 10 into a number of any basenewBase
as string representation and splits the number into blockt with given length.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, which will the numberdecValue
be 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 parameterdecValue
isnull
NumberFormatException
- If the parameterdecValue
is not a number of base 10IllegalArgumentException
- IfdecValue
is an empty string ornewBase
leaves the range of values [2, 36] - seeisValueToBase(int,java.lang.String)
NumberOverflowException
- ifvalue
is 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 ofvalue
value
- 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, IllegalArgumentException
Checks if the submitted stringvalue
represents a number to the required basebase
and throws an exception, if this is not given.- Parameters:
base
- Base which isvalue
checked forvalue
- String which is proofed- Throws:
NullPointerException
- Ifvalue
isnull
NumberFormatException
- If the parametervalue
is not a number of basebase
IllegalArgumentException
- Ifbasis
leaves the range of value [2, 36] orvalue
is an empty string
-
baseToDecIntPart
private static double baseToDecIntPart(int base, String integerPart, Queue<? super HornersMethod> calcSteps)
Converts the submitted numberintegerPart
of any basebase
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 ofintegerPart
in the range [2; 36]integerPart
- Number which will be converted as string representationcalcSteps
- Queue to store every calculation step,null
is 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 offractionalPart
in 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,null
is 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,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 classPreferences
the 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,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 stringvalue
, which represents a number of the basebase
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
- Ifvalue
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
null
if 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
-
-