Results 1 to 10 of 10
  1. #1
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default Converting double to char array?

    I have to detect whether a double is a fraction or not.

    So, coming from a C background, I thought I should convert the double to an array of chars and check to see if theres any non-zero numbers to the right of the decimal place.

    Is this the right way to go about it? If it is, how would i do it?

    I don't think I'm allowed to import any packages.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Blasz View Post
    I have to detect whether a double is a fraction or not.

    So, coming from a C background, I thought I should convert the double to an array of chars and check to see if theres any non-zero numbers to the right of the decimal place.

    Is this the right way to go about it? If it is, how would i do it?

    I don't think I'm allowed to import any packages.
    Check out the (static) methods in the Math class; if x == Math.ceil(x) there is no fractional part in x; equivalent: if x == Math.floor(x) there is also no fractional part in x.

    kind regards,

    Jos

  3. #3
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Thanks man, just a few more questions (I only started learning Java today).

    Where do I check out these methods in math class (or any class).

    My JRE System library is jdk1.6.0_12, does that affect how many classes I can use?

    Is the Math class a basic in-built class that is attached to every java library?

    Where can I find all the different classes for my JRE System library (assuming that is where all the classes come from?)

    Yes, I'm very noob at java.

    EDIT: Also, with your example wouldn't it be x + 1 == Math.ceil(x) or x = Math.ceil(x+1)?
    Last edited by Blasz; 08-29-2010 at 01:20 PM. Reason: extra question

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Blasz View Post
    Thanks man, just a few more questions (I only started learning Java today).

    Where do I check out these methods in math class (or any class).

    My JRE System library is jdk1.6.0_12, does that affect how many classes I can use?

    Is the Math class a basic in-built class that is attached to every java library?

    Where can I find all the different classes for my JRE System library (assuming that is where all the classes come from?)

    Yes, I'm very noob at java.

    EDIT: Also, with your example wouldn't it be x + 1 == Math.ceil(x) or x = Math.ceil(x+1)?
    You can bookmark or download the stuff from this link. Java 1.6 is the latest version so you are up to date. Never program anything without checking the API documenation. The Math class is in the java.lang package and that package is included implicitly so you don't have to import anything. For starters read the description of the Math.ceil( ... ) method and you'll see that if x == Math.ceil(x) there is no fractional part in x.

    kind regards,

    Jos

  6. #6
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    True, thankyou so much.

  7. #7
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Ok turns out I probably will need to convert a double to a char array as I need to know how many digits there are to the right of the decimal place.

    How would I do this?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Blasz View Post
    Ok turns out I probably will need to convert a double to a char array as I need to know how many digits there are to the right of the decimal place.

    How would I do this?
    Think of it: how many decimals are there in the fraction part of 3.000000? Maybe you should check one of the Formatter classes such as the DecimalFormat class ...

    kind regards,

    Jos

  9. #9
    Blasz is offline Member
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Hmm, well this is for a different part of the program. I will need to know how many zero's are right of the decimal if they are all entered but I'm guessing double would just cut them off and keep the value equal to 3.0?

    I had a look at DecimalFormat but it looks very daunting, could you explain a little bit of what it does / how I could use it?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,570
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Blasz View Post
    Hmm, well this is for a different part of the program. I will need to know how many zero's are right of the decimal if they are all entered but I'm guessing double would just cut them off and keep the value equal to 3.0?

    I had a look at DecimalFormat but it looks very daunting, could you explain a little bit of what it does / how I could use it?
    Well, that class returns a String given a 'format specifier' (also a String) telling it how to format a number and a number to format. Try it and when/if you're stuck ask questions about it here.

    kind regards,

    Jos

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  3. Converting a char to an int
    By michail in forum New To Java
    Replies: 10
    Last Post: 01-06-2010, 09:09 PM
  4. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 06:01 PM
  5. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 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
  •