Friday, 15 February 2013
JAVA: Parse String to Double
Do you like this Article?
Paser String to Double:
You can parse a double value in string format to double in 4 ways.
- Double.valueof(String s) : This method returns a double object containing the value invoked in it in string format.
- It can trim white-space in start or end of the string argument, by using String.trim().
- It throws NumberFormatException if the string argument is not in proper form.
- Double.parseDouble(String s) : This method also returns a double instance containing the invoked string value.
- It can also trim white space in start and end of the input value, using String.trim().
- It also throws NumberFormatException if the string argument is not in proper form.
- new Double(String paramString) : You a also parse a string argument containing float value by invoking it on the constructor of the Class Double. its internally using the valueof(String s) method and returns a new Double instance containing the parsed double value. So it is also using
String.trim() and throws NumberFormatException. - Fourth one, the last one is the very important one, actually its been used by all the above 3 APIs. sun.misc.FloatingDecimal.readJavaFormatString(String paramString). Its the original method used to parse a String value to decimal value. This is the actual methods, which is trimming the white space of the string, using String.trim(). It returns value in FloatingDecimal instance, so you need to cast it to double by using doubleValue(), which is another method of Class FloatingDecimal.
Code:
package
com.interview.question.java;
public class
PaserStringToDouble {
/**
*
*
Sample class that shows how to parse a String to double.
*
* @author
http://interviewquestionjava.blogspot.com
*
*/
public static void main(String[]
args) {
// TODO
Auto-generated method stub
// set a decimal value in a string
with
// white space on both end.
String strValue = " 12.329
";
// parse the string to double using
//Double.valueof(String s)
double dblValue =
Double.valueOf(strValue);
// parse the string to double using
// Double.parseDouble(String s)
double dblValue2
= Double.parseDouble(strValue);
// parse the string to double using
//Double(String
paramString)
Double dblValue3 = new
Double(strValue);
// parse the string to double using
new
//
FloatingDecimal.readJavaFormatString
//(String paramString)
Double dblValue4 = new
Double(sun.misc.FloatingDecimal
.readJavaFormatString(strValue).doubleValue());
// printing the outputs
System.out.println("dblValue:
" + dblValue +
"\ndblValue2:
" + dblValue + "\ndblValue3: " +
dblValue3 + "\ndblValue4: " + dblValue4);
}
}
Output:
dblValue: 12.329
dblValue2:
12.329
dblValue3:
12.329
dblValue4:
12.329
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
public - public means everyone can access it.That means it's in global scope. As, main method is called by JVM [ Java Virtual Machine...
-
throw is used to throw an exception in a program, explicitly . Whereas, throws is included in the method's declaration part, wi...
-
Singleton in one of the most popular yet controversial design pattern, in the world of object oriented programming. It's one of t...
-
Web Container / Servlet Container / Servlet Engine : In J2EE Architecture , a web container (also known as servlet container or ser...
-
Program compiles. But at runtime throws an error “NoSuchMethodError”.
-
Vector : It's synchronized. It's slower than ArrayList. It's generally used in ...
-
doGet(): protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, java.io.IOException – is a met...
-
In Java Programming Language , we must declare a variable name and type, before using it. The data type of a variable defines the th...
0 Responses to “ JAVA: Parse String to Double ”
Post a Comment