This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 6 years ago.
I wanted to divide the integer and store it in an array
For Ex:1000000000000 into two indexes
arr[0]=1000000
arr[1]=000000
but arr[1] stores it as 0 instead of 0000000.
I wanted to perform some operations with it,so i needed 7 zeros in it ,instead of 1 zero.
Is it achievable in some way ?
Convert your number to a string then use substring() to split it up.
long num = 1000000000000L;
String str = num + "";
String[] array = new String[2];
array[0] = str.substring(0, 6);
array[1] = str.substring(7);
Related
This question already has answers here:
Convert an integer to an array of digits
(24 answers)
Closed 2 years ago.
I would just like to ask how to convert an int to int array - for example:
int number = 12345;
to:
[1,2,3,4,5];
Btw - could not really find anything out there, so we may hope that someone know.
thanks in advance
You can do it like this:
int number = 12345;
int[] digits = String.valueOf(number).chars().map(c -> c-'0').toArray();
Explanation:
First convert int to string with String.valueOf(number) to be able to use the method chars() which get a stream of int that represents the ASCII value of each char in the string. Then, we use the map function map(c -> c-'0') to convert the ASCII value of each character to its value, subtracting the value of the ASCII code from the character '0' from the ASCII code of the actual character. Finally, the stream is converted to an array with toArray().
This question already has answers here:
How can I convert a char to int in Java? [duplicate]
(4 answers)
Java: parse int value from a char
(9 answers)
Closed 3 years ago.
I was experimenting with char arrays and integers, when I found something that confused me a lot. In Java, if you type the following code:
String inputs = "123";
char[] inputs2 = inputs.toCharArray(); // Turn String into Char Array
int a = inputs2[0];
println(a);
I would have expected for "a" to return 1 because 1 is the first element in the array. However, it returned 49. Why does this occur? Is there any way to get around this issue?
This question already has answers here:
Java reverse an int value without using array
(33 answers)
Closed 4 years ago.
This is the logic to reverse a number. But this logic doesn't reverse the number having 0. for example i want to reverse 70 means it will give output as 7.
so kindly give logic to reverse number having 0. Thanks.
while(num>0)
{
rem=num%10;
sum=(sum*10)+rem;
num=num/10;
}
System.out.println("Output is:"+sum );
Integer numbers cannot have leading zeroes.
What would be the use case in which such a function be useful ?
If the purpose is just to reverse an integer number and use it as a string,
then this should do the job.
int num = 70 ;
String numStr = new Integer(num).toString();
StringBuilder numStrB = new StringBuilder(numStr);
String reversedStr = numStrB.reverse().toString();
This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
int number = 24.24;
int afterDot = (int) (number*100)%100;
afterDot = 24
This logic is wrong if
int number1 = 24.4
How can I get the 4 from 24.4 ?
Whatever will be the number but want to extract the value after dot.
Actually the formula has to work for both type of value.
If you convert your double to a string, the problem becomes easier :
double number = 24.4;
String numberAsString = String.valueOf(number);
String decimalPart = numberAsString.split("\\.")[1];
System.out.println(decimalPart);
int number1 = Integer.valueOf(decimalPart); // NOTE: This conversion is lossy.
System.out.println(number1);
Note that by converting your decimalPart string (e.g. "001") to an integer (1), you might lose some information.
With 24.4, it outputs :
4
4
With 24.001, it outputs :
001
1
With 3d, it outputs:
0
0
This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I want to print the two integer variables divided.
int a = 1, b = 2;
System.out.println(a + b);
Obviously println() function processes them as integers and calculates the sum.
Instead I would like the output becomes like this "12". Any ideas?
Insert some blank Strings to induce this:
System.out.println(a +""+ b);
Store the integers as strings:
String a = "1";
String b = "2";
String c = a + b;
System.out.println(c);