Results 1 to 7 of 7
  1. #1
    coolsig is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default convert a really big string (len 39) to bigdecimal

    Hi ,
    How do I convert a large String (length 39 ) to BigDecimal ?

    eg
    String c = "848595950484765868574748349586738596879";

    try {
    BigDecimal num2 = BigDecimal.valueOf((Long.parseLong(c)));
    } catch (NumberFormatException nfe) {
    nfe.printStackTrace();
    }

    I tried this, it throws a nfe. Any ideas on how to get this string parsed to an BigDecimal.

    Thanks

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    try biginteger
    My IP address is 127.0.0.1

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    The way you initialize BigDecimal is strange for me....

    I guess you should initialize first an Instance of it before you use valueOf(something) method....

    But it may be simplified....
    According to your string sample, that value may not be applicable to long, see what's the maximum value can long datatype handles...

    You may also try Double,

    eg.

    Java Code:
    String c = "848595950484765868574748349586738596879";
           try {
                BigDecimal num2 = new BigDecimal(new Double(c));
                System.out.println(num2);
           } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
           }
    Last edited by sukatoa; 06-07-2008 at 03:52 PM. Reason: Typo
    freedom exists in the world of ideas

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

    Default

    Java Code:
    import java.math.BigDecimal;
    
    public class Test {
        public static void main(String[] args) {
            String c = "848595950484765868574748349586738596879";
            BigDecimal num2 = null;
            try {
                num2 = new BigDecimal(c);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }
            System.out.println("num2 = " + num2);
        }
    }

  5. #5
    coolsig is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks,
    It worked :)

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    You may now mark this thread as SOLVED....
    freedom exists in the world of ideas

  7. #7
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 02:32 PM
  2. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 04:46 PM
  3. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 AM
  4. convert string to float
    By miss_dot in forum NetBeans
    Replies: 1
    Last Post: 11-15-2007, 12:26 AM
  5. Error: convert from String to long
    By bbq in forum New To Java
    Replies: 1
    Last Post: 06-29-2007, 08:23 PM

Posting Permissions

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