Integer & Long
The Log Formula | The Exponent formula |
---|---|
\[\log_{base}{index} = {power}\] | \[base^{power} = index\] |
Example : \(\log_{10}{100} = 2\) | Example : \(10^{2} = 100\) |
The Log Formula :
$$ \log_{base}{index} = {power} $$
The Exponent formula :
$$ base^{power} = index $$
Counting the number of digits
$$ \text{number of digits, n} = \lfloor \log_{10}(x) \rfloor + 1 \text{, where x is an integer number} $$
int num = 123456789;
int numberOfDigits = (int) Math.floor(Math.log10(num)) + 1;
//Using String Length
int numberOfDigitsWithString = String.valueOf(num).length();
Primitive with parse
parseInt
& parseDouble
: Used for parsing a String to a primitive int or
double.
int i = Integer.parseInt("12345");
int j = Integer.valueOf("1234").intValue();
int k = Integer.valueOf(23).intValue();
Wrapper using valueOf
valueOf
is Used for creating instances of wrapper classes from String or
primitive to Wrapper
Integer fromPrimitiveInt = Integer.valueOf(25);//From String or primitive to Wrapper
Integer fromString = Integer.valueOf("90");
intValue and Auto unboxing
intValue
: Used to retrieve the primitive int value from a wrapper object.
- Not null safe. Throws NumberFormatException when null is used
Integer fromPrimitiveInt = Integer.valueOf(25);//From primitive to Wrapper
Integer fromString = Integer.valueOf("25");// From String
int i1 = fromPrimitiveInt.intValue();
atoi() - String to Int without parseInt
Longer Approach $$ 12345 = 1\times10^4 + 2\times10^3 + 3\times10^2 + 4\times10^1 + 5 \times 10^0 $$
digit = (int) (digit + (x * Math.pow(10, str.length() - 1 - i)));
Left to Right String Processing. The C language atoi() function converts a character string to an integer value.
$$ 12345 = 1\times10 \rightarrow 10+2 \rightarrow (12\times10)+3 \rightarrow (123\times10)+4 \rightarrow (1234\times10)+5 $$
digit = (digit * 10) + x;