문자를 숫자로 변환하기 Converting Strings to Numbers
빈번하게, 프로그램은 문자열 객체 안의 숫자 데이터로 끝나는데, 예를 들명 사용자가 입력 한 값으로 끝납니다.
원시 숫자 타입(Byte
, Integer
, Double
, Float
, Long
및 Short
)을 둘러싼 서브클래스 Number들은 문자열을 해당 타입의 객체로 변화하는 valueOf 라는 이름의 클래스 메소드를 제공합니다. 객체에 문자열을 변환 클래스의 방법을 제공한다. 명령 줄에서 2개의 문자열을 얻는, 예제 ValueOfDemo
는 그들을 숫자로 변환시키고, 그 값들 위에서 연산을 수행합니다:
Frequently, a program ends up with numeric data in a string object—a value entered by the user, for example.
The Number
subclasses that wrap primitive numeric types ( Byte
, Integer
, Double
, Float
, Long
, and Short
) each provide a class method named valueOf
that converts a string to an object of that type. Here is an example, ValueOfDemo
, that gets two strings from the command line, converts them to numbers, and performs arithmetic operations on the values:
public class ValueOfDemo {
public static void main(String[] args) {
// this program requires two
// arguments on the command line
if (args.length == 2) {
// convert strings to numbers
float a = (Float.valueOf(args[0])).floatValue();
float b = (Float.valueOf(args[1])).floatValue();
// do some arithmetic
System.out.println("a + b = " +
(a + b));
System.out.println("a - b = " +
(a - b));
System.out.println("a * b = " +
(a * b));
System.out.println("a / b = " +
(a / b));
System.out.println("a % b = " +
(a % b));
} else {
System.out.println("This program " +
"requires two command-line arguments.");
}
}
}
The following is the output from the program when you use 4.5
and 87.2
for the command-line arguments:
a + b = 91.7 a - b = -82.7 a * b = 392.4 a / b = 0.0516055 a % b = 4.5
Note: Each of the
Number
subclasses that wrap primitive numeric types also provides a parseXXXX()
method (for example, parseFloat()
) that can be used to convert strings to primitive numbers. Since a primitive type is returned instead of an object, the parseFloat()
method is more direct than the valueOf()
method. For example, in the ValueOfDemo
program, we could use:float a = Float.parseFloat(args[0]); float b = Float.parseFloat(args[1]);
Converting Numbers to Strings
Sometimes you need to convert a number to a string because you need to operate on the value in its string form. There are several easy ways to convert a number to a string:
int i; // Concatenate "i" with an empty string; conversion is handled for you. String s1 = "" + i;
or
// The valueOf class method. String s2 = String.valueOf(i);
Each of the Number
subclasses includes a class method, toString()
, that will convert its primitive type to a string. For example:
int i; double d; String s3 = Integer.toString(i); String s4 = Double.toString(d);
The ToStringDemo
example uses the toString
method to convert a number to a string. The program then uses some string methods to compute the number of digits before and after the decimal point:
public class ToStringDemo { public static void main(String[] args) { double d = 858.48; String s = Double.toString(d); int dot = s.indexOf('.'); System.out.println(dot + " digits " + "before decimal point."); System.out.println( (s.length() - dot - 1) + " digits after decimal point."); } }
The output of this program is:
3 digits before decimal point. 2 digits after decimal point.
'Java 배우기' 카테고리의 다른 글
Comparing Strings and Portions of Strings 문자열과 문자열 일부의 비교 (0) | 2016.01.05 |
---|---|
Manipulating Characters in a String String 안에서 글자 조작하기 (0) | 2016.01.05 |
Strings 문자열 (0) | 2016.01.05 |
Characters 글자 (0) | 2016.01.05 |
Questions and Exercises: Numbers 질문과 연습: 숫자 (0) | 2016.01.05 |
댓글