Results 1 to 2 of 2
  1. #1
    srini is offline Member
    Join Date
    Dec 2007
    Posts
    1
    Rep Power
    0

    Default Converting String to Double

    Hi All,
    Can any one help me.When I converting String to long ,It shwoing Exponent value and throwing numberformat exception.
    I want out put like this 99998900001.00.
    I have given input like this 99998900001.00.

    String stotal=request.getParameter("amt");
    sop( stotal);
    long damount=Long.parseLong(stotal);
    sop( damount);

    stotal= 99998900001.00
    throw numberformateexception
    damount = 9.9998900001E10
    Last edited by srini; 12-24-2007 at 12:08 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    By default in java, any number with a decimal in it is considered/read/treated as a double data type. To get this into a long data type you would have to cast it to a long. But then you won't get it to print out as a double, ie, with two decimal places.
    Java Code:
    public class Test {
        public static void main(String[] args) {
            String stotal = "99998900001.00";
            // NumberFormatException
    //        long error = Long.parseLong(stotal);
            double okay = Double.parseDouble(stotal);
            System.out.printf("okay = %.2f%n", okay);
            long toLong = (long)okay;
            long again = Double.valueOf(stotal).longValue();
            System.out.printf("toLong = %d  again = %d%n",
                               toLong, again);
        }
    }

Similar Threads

  1. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  2. Converting URL to URI
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:15 AM
  3. Converting Epoch to string Date
    By amyedwards in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:33 PM
  4. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 03:10 AM
  5. help with converting to JApplet
    By Simmy in forum AWT / Swing
    Replies: 2
    Last Post: 08-09-2007, 08:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •