forked from Nnamdikeshi/Java2545examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberStringConversions.java
More file actions
62 lines (31 loc) · 1.81 KB
/
Copy pathNumberStringConversions.java
File metadata and controls
62 lines (31 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package week1_variables_if_else;
/**
* Created by admin on 8/16/16.
*/
public class NumberStringConversions {
public static void main(String[] args) {
//To convert an int variable to a String
int anInt = 6;
String stringFromInt = Integer.toString(anInt);
System.out.println("numberString = " + stringFromInt);
//Or, a hack :)
String stringFromIntAnotherWAy = "" + anInt; //empty String plus number - Java will convert the int to a String and join the two together
System.out.println("stringFromIntAnotherWAy = " + stringFromIntAnotherWAy);
//To convert a String to an int
String intString = "98765"; // A String that represents a int.
int intFromString = Integer.parseInt(intString);
// Warning! Your program will crash if the String can't be interpreted as an int number. So Strings like "123.45" or "Ten" will produce an error.
System.out.println("intFromString = " + intFromString);
//To convert double variable to a String
double aDouble = 12.34;
String stringFromDouble = Double.toString(aDouble);
System.out.println("stringFromDouble = " + stringFromDouble);
//Or, a hack :)
String stringFromDoubleAnotherWay = "" + aDouble; //empty String plus number - Java will convert the double to a String and join the two together
System.out.println("stringFromDoubleAnotherWay = " + stringFromDoubleAnotherWay);
//To convert a String to a double
String doubleString = "123.45"; // A String that represents a double.
double doubleFromString = Double.parseDouble(doubleString); // Warning! Your program will crash if the String can't be interpreted as a number.
System.out.println("doubleFromString = " + doubleFromString);
}
}