Results 1 to 5 of 5
  1. #1
    shadowfax57 is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default Binary-to-Decimal Conversion (Precision Problem)

    This semester I'm taking a class in Number Theory and Intro to Java. As a way to study both I'm trying to make a program that can convert values from one number sytem (ie binary, hexadecimal, octal) to decimal.

    One step in this has me needing to convert a double (the number to be converted) to a string. From there I use .charAt(int) to pick each digit and multiply it by the correct power of 2 .
    (ie: ABCD = a*2^3 + b*2^2 + c*2^1 + d*2^0)

    When I do convert the double to a string, the portion past the floating point is changed slightly. For example, the number I enter to be converted from binary to decimal as 10110.1101 is rounded by the computer in the conversion process to the value 10110.110099 (which obviously doesn't play by the rules of binary lol).

    So I'm looking for a way to stop the value from being changed, during the conversion to string process. Or I'd like to find a way around having to convert to a string in the first place?

    Any ideas are greatly appreciated.
    I've already tried double=Math.floor(double*1000)/1000, and a few other normally clever fixes, but to no success.

    Thanks again for your help.
    Last edited by shadowfax57; 09-06-2010 at 07:18 PM. Reason: misspelling

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Forget about raising numbers to a power and have a look at the Integer class; it has all the methods you want or need.

    kind regards,

    Jos

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

    Default

    Is your project to write methods that are available in the Integer class
    or is it to do something at a higher lever using the Integer class methods?

    a way to stop the value from being changed
    Don't use floating point numbers. Do it all in whole numbers.

    converted from binary to decimal as 10110.1101 is rounded by the computer in the conversion process to the value 10110.110099
    How did that work? Would the decimal value would be: (16+4+2).(1/2+1/4+1/16)

  4. #4
    shadowfax57 is offline Member
    Join Date
    Aug 2010
    Posts
    8
    Rep Power
    0

    Default

    Well to be honest I was looking to do it w/o any pre-written methods, simply because it would force me to take a longer time with it and get familiarized w/ the language. I will take a look at the Int class though, I'm sure it's definitely important enough and well worth the look.

    Thanks

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Is the variable that you're sticking this in a double?
    That is, is this "10110.110099" in a double?

Similar Threads

  1. Convert Decimal To Binary
    By aspire007 in forum New To Java
    Replies: 8
    Last Post: 08-06-2010, 07:32 AM
  2. Binary to Decimal Converter
    By c_walker in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 02:38 PM
  3. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  4. Eclipse- Decimal to binary
    By queen_vee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 02:17 PM
  5. Replies: 7
    Last Post: 01-12-2008, 09:02 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
  •