Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2007, 07:14 AM
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
Converts a binary number to a decimal
I need to write a method that converts a binary number to a decimal. Here is what i have so far any suggestions on how i can get this to work right now it returns 147.0 when you enter the binary number 11 which should have the decimal equivalent of 3.

Does anyone know where i messed up? I've been working on just this method for probably 8 hours including yesterday and today and i don't think I'm even close.

Code:
static double toDecimal (String s) { int l = s.length(); double result = 0; for (int i = 0; i < l; i++) { result = result + s.charAt(i) * Math.pow(2, (s.length() - i - 1)); } return result; }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2007, 10:57 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,015
hardwired is on a distinguished road
We only want to add the values of the bits that are turned on
and to ignore the zero/off bits.
Code:
private static double toDecimal(String s) { int l = s.length(); double result = 0; for (int i = 0; i < l; i++) { if(s.charAt(i) == '1') result = result + Math.pow(2, (s.length() - i - 1)); } return result; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java calculator decimal cart1443 New To Java 2 04-16-2008 02:19 PM
Capping decimal places Rageagainst20 New To Java 1 12-20-2007 10:28 PM
rounding double to two decimal places javaMike Advanced Java 1 11-27-2007 04:58 AM
round to two decimal places javaMike New To Java 1 11-26-2007 10:46 PM
Converts from Fahrenheit to Celsius trill New To Java 1 08-06-2007 06:52 AM


All times are GMT +3. The time now is 03:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org