Αποτελέσματα Αναζήτησης
The way I know how to convert an integer into a string is by using the following code: Integer.toString(int); and . String.valueOf(int); If you had an integer i, and a string s, then the following would apply: int i; String s = Integer.toString(i); or String s = String.valueOf(i);
Alternatively, you can use an Ints method from the Guava library, which in combination with Java 8's Optional, makes for a powerful and concise way to convert a string into an int: import com.google.common.primitives.Ints; int foo = Optional.ofNullable(myString) .map(Ints::tryParse) .orElse(0)
5 Νοε 2010 · This is not a good way to convert an int to a string. Actually, you are not converting an int to a string, you are converting an int to a char. This will interpret the int as an ASCII character code and give you that char. It's dangerous (negative values, etc.) and overall just plain wrong. –
9 Μαρ 2010 · if the int value is 15, you can convert it to a binary as follows. int x = 15; Integer.toBinaryString(x); if you have the binary value, you can convert it into int value as follows. String binaryValue = "1010"; Integer.parseInt(binaryValue, 2);
16 Μαρ 2011 · In Java, you really want to use Integer.toString to convert an integer to its corresponding String value. If you are dealing with just the digits 0-9, then you could use something like this: If you are dealing with just the digits 0-9, then you could use something like this:
31 Μαΐ 2017 · int someValue = 42; Now I want to convert that int value to a String. Which way is more efficient? // One String stringValue = Integer.toString(someValue); // Two String stringValue = String.valueOf(someValue); // Three String stringValue = someValue + ""; I am just curious if there is any real difference or one is better than the other?
2 Φεβ 2010 · He was asking for an integer string, so the line converting to an int should be something along the lines of String str = Integer.tostring((int)floatVar); – Håvard S Commented Feb 2, 2010 at 9:31
26 Δεκ 2011 · Similarly, one of the easiest way to convert primitive datatypes to String is to use the toString() method with the datatype object of the element to be converted. String str = Double.toString(d); // Convert double to String String str = Long.toString(l); //Convert long to String String str = Float.toString(f); //Convert float to String
30 Ιαν 2011 · String s = String.valueOf(i); String s = Integer.toString(i); Another more concise way is: String s = "" + i; See it working online: ideone. This is particularly useful if the reason you are converting the integer to a string is in order to concatenate it to another string, as it means you can omit the explicit conversion:
I am wondering if it is possible, using the String.format method in Java, to give an integer preceding zeros? For example: 1 would become 001 2 would become 002 ... 11 would become 011 12 would b