AllExperts > Java 
Search      
Java
Volunteer
Answers to thousands of questions
 Home · More Java Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Brian Ngure
Expertise
Strong in Java. Will be able to answer both in areas of designing and coding. Some knowledge in servlets

Experience
5 years of Java experience, Sun Certified Java Associate and Sun Certified Java Programmer, JDK 1.4

 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Double value Handling in java

Java - Double value Handling in java


Expert: Brian Ngure - 8/26/2008

Question
I am getting a Value from query as "53310588.75" as String.
When i convert into double value using Double.parseDouble("53310588.75") it gives a value as "5.331058875E7".
How can i pass this value in a procedure that accept parameter as Number.

Answer
Hi,

You need to use the NumberFormat and DecimalFormat classes to format the number the way you want.

e.g

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class DecimalFormatExample
{
   public static void main(String[] args)
   {
       // We have some millons money here that we'll format its look.
       double money = 100550000.75;
       
       // By default to toString() method of the Double data type will print
       // the money value using a scientific number format as it is greater
       // than 10^7 (10,000,000.00). To be able to display the number without
       // scientific number format we can use java.text.DecimalFormat wich
       // is a sub class of java.text.NumberFormat.
       
       // Below we create a formatter with a pattern of #0.00. The # symbol
       // means any number but leading zero will not be displayed. The 0
       // symbol will display the remaining digit and will display as zero
       // if no digit is available.
       NumberFormat formatter = new DecimalFormat("#0.00");
       
       // Print the number using scientific number format.
       System.out.println(money);
       
       // Print the number using our defined decimal format pattern as above.
       System.out.println(formatter.format(money));
   }
}


Brian

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.