There are two ways to convert Strings into Number :
1. 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 anObject of that type.
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
2. 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.
Example :
-Above ValueOfDemo program, we could use :
float a = Float.parseFloat(args[0]);
float b = Float.parseFloat(args[1]);
1. 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 anObject of that type.
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
2. 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.
Example :
-Above ValueOfDemo program, we could use :
float a = Float.parseFloat(args[0]);
float b = Float.parseFloat(args[1]);
we can use static methods of wrapper classes to convert strings into primitive numbers
ex:int i= Integer.parseInt("10");
sop(i)-->10
float i= Float.parseFloat("10.6");
ex:int i= Integer.parseInt("10");
sop(i)-->10
float i= Float.parseFloat("10.6");
Double.parseDouble(String argument);
Converting Numbers into Strings:
Converting Numbers into Strings:
Sometimes we 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 :
Example : int i;
String s1 = "" + i; //Concatenate "i" with an empty string;
or
String s2 = String.valueOf(i); //The valueOf class method.
- Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a String.
Example :
int i;
double d;
String s3 = Integer.toString(i);
String s4 = Double.toString(d);
Example : 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.
There are several easy ways to convert a number to a String :
Example : int i;
String s1 = "" + i; //Concatenate "i" with an empty string;
or
String s2 = String.valueOf(i); //The valueOf class method.
- Each of the Number subclasses includes a class method, toString(), that will convert its primitive type to a String.
Example :
int i;
double d;
String s3 = Integer.toString(i);
String s4 = Double.toString(d);
Example : 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.
No comments:
Post a Comment