point on binary to decimal

i dont know how to put the decimal point function to my binary to dec.

Page 1 of 1

4 Replies - 1060 Views - Last Post: 21 June 2010 - 04:20 PM Rate Topic: -----

#1 punksryan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 31-January 10

point on binary to decimal

Posted 21 June 2010 - 03:27 AM

import java.util.Scanner;
public class BinarytoDecimal {

    public static void main(String[] args) throws Exception {
      Scanner br = new Scanner(System.in);
        System.out.println("Enter the binary value:");
         String s = br.nextLine();
        System.out.println("Decimal value is : "+Integer.parseInt(s, 2));

    }
}



i want to study this program coz it is related to our subject in logsign... i wish you can help me guys...

Is This A Good Question/Topic? 0
  • +

Replies To: point on binary to decimal

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,984
  • Joined: 20-September 08

Re: point on binary to decimal

Posted 21 June 2010 - 05:13 AM

What's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: point on binary to decimal

Posted 21 June 2010 - 06:51 AM

Doesn't seem to be a problem to me. You say you want to study the program, so are you asking what's happening?

I'll write comments on exactly what's happening:

import java.util.Scanner;//import Scanner for use with input
public class BinarytoDecimal {

    public static void main(String[] args) throws Exception {
      Scanner br = new Scanner(System.in);//Scanner class used to get input
        System.out.println("Enter the binary value:");//prints this message on screen
         String s = br.nextLine();//set s equal to whatever the user inputs
        System.out.println("Decimal value is : "+Integer.parseInt(s, 2));
        //^print out "Decimal value is : " + the string the user inputted,
        //parsed to a decimal value. The second parameter is the radix

    }
}


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,255
  • Joined: 27-December 08

Re: point on binary to decimal

Posted 21 June 2010 - 04:17 PM

Please post your question in the body of your post, not in the title where it gets truncated. As Integer.parseInt() returns an int, no decimal point will be shown. However, you can cast it to a double, which will convert it to a floating-point number, or a very close representation to a real number (meaning that 1.41 might be represented as 1.4019999999999).
Was This Post Helpful? 0
  • +
  • -

#5 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 144
  • Joined: 30-October 07

Re: point on binary to decimal

Posted 21 June 2010 - 04:20 PM

I'm not sure what you're asking either.
But if your curious as to how the value goes from base 2 to base 10, take a look at the src of the parseInt method.


Humans do something along the following:
For each digit n starting from the right, multiple the value of the digit by the base to the n-1 power.
Add these results together and you get base-10.

For example 11010101 would be
1 * 2^0 = 1
0 * 2^1 = 0
1 * 2^2 = 4
0 * 2^3 = 0
1 * 2^4 = 16
0 * 2^5 = 0
1 * 2^6 = 64
1 * 2^7 = 128
-----------
11010101 = 213

so to do base 16 [hexadecimal]
Instead of 0123456789 10 11 12 13 14 15
We use 0123456789ABCDEF

03FA to decimal is:
A [10] * 16^0 = 10
3 [03] * 16^1 = 48
F [15] * 16^2 = 3840
0 [00] * 16^3 = 0
-----------------
0F3A = 3898

This post has been edited by DillonSalsman: 21 June 2010 - 04:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1