Solved

how to parse string to double upto two decimal digits

Posted on 2013-05-22
7
515 Views
Last Modified: 2013-07-02
Parse string to double returns me the double value upto one decimal digit.
How to get the double value upto two decimal digits. I want it to be 1.00and not 1.0

String Amt = "1.00";
Double Price = Double.parseDouble(Amt);

Ouput:- 1.0
0
Comment
Question by:Newton21205
7 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 39189357
Numbers (double/Double in your case) don't have formats, so you have what is known as a 'category error'. Only strings have formats
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39189360
import java.text.*;
import java.math.*;


class DecFormat {

static DecimalFormat df;
static BigDecimal bd;
static Number num;
static Double d;
static Float dd;

static final double FNUM = 123456.00;




	public static void main(String[] args){


double dub = 12345678.00;
DecimalFormat def = new DecimalFormat();

def.applyPattern("##,###,###.##;(##,###,###.##)");

def.setMinimumFractionDigits(2);

//def.setRoundingMode(RoundingMode.valueOf("HALF_DOWN") );//cant get this to work

System.out.print(def.format(dub));

System.exit(0);
		DecimalFormat f = new DecimalFormat("###########0.00");
		
		df = new DecimalFormat();
		//df = new DecimalFormat();
		//df.setMinimumFractionDigits(2);
				
		//df.applyPattern("##,###,###.00;(##,###,###.00)");
		

				//df.setRoundingMode(RoundingMode.valueOf("UNNECESSARY") );

		//num = df.parse("12345678.00",new ParsePosition(0));
		//num = f.parse("12345678.00",new ParsePosition(0));
num = f.parse("123456.00",new ParsePosition(0));
Float fl = new Float(num.floatValue());				
		System.out.println(fl);
		

	}


}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39189369
String Amt = "1.00";
double d = Double.parseDouble(Amt); // Don't use capitals for variable names in Java
String amt = String.format("%.2f", d);
System.out.println(amt); // But it's now a String again

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Newton21205
ID: 39189588
@CEHJ : i want a double not a string
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39189718
What CEHJ is saying and trying to point out to you, is that a "double" has NO concept of "number of decimal places", ie. 1.00 = 1.0 = 1.00000000 = etc, etc. It is ONLY when it is converted to a String either explicitly using String.format, etc or implicitly (see below) that the number of decimal places that you want included in the String is important.

What do I mean by "converted implicitly" above? Well, in your original question, the last line says, Output:- 1.0. How did you get to that conclusion? I am assuming that at some point you did something along the lines of, System.out.println(Amt);??? If so, then in that one line, an implicit conversion to a String occurs. It has to because to write to the console, which is just a stream of characters, a number such as Amt needs to be converted to it's textual representation. "System.out" is actually a PrintStream object, and if you follow the trail of Javadoc references for the PrintStream .println(double) method, you will see those references describing this implicit conversion and ending up at Double.toString(double). If you read the description of the method in that Javadoc you will see why it printed "1.0" to the console.


So, what should YOU do then? Well, just use your Double everywhere that you need to representation a decimal number (of any number of decimal places), ie. add it to others, subtract, multiply, divide, whatever you want to do with it. And then AFTER you do all your mathematical operations, and you then want to print the result to the console, that's when you use the last two lines of CEHJ's post to "explicitly" convert the result to a String (that way YOU control the number of decimals included) and then print the String to the console.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39190284
@CEHJ : i want a double not a string
What is the mathematical difference between 1.0 and 1.00? Answer: there isn't one, which is why a number has no need of, and no means of, distinguishing between the two.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 250 total points
ID: 39190351
I posted the wrong code above, so for the record, this is what I meant to post :

String du = "12345678.00";
double dub = new Double(du);//12345678.00;
DecimalFormat def = new DecimalFormat();

def.applyPattern("##,###,###.00;(##,###,###.00)");

System.out.print(def.format(dub));

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 18
split53 challenge 7 77
Java Server Faces parameter pass? 6 37
diffSum example 4 20
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now