-1

I need to create a program that converts a string to double.

public class Convert{
  public static void main(String args[]){
    String num="124023211.123";
    int d=0,g=0,c=0,fnl=0,h=0,v=0;
    double fnl2=0;
    int exp=(num.indexOf(".")-1);
    while(num.charAt(d)!='.'){
        g=num.charAt(d)-48;
        int k = 1;
        for(int f=0;f<exp;f++){
            k=(k*10);
        }
        fnl+=(k*g);
        d++;
        exp--;
    }
    num=(num.substring(d+1) );
    //System.out.println(fnl);
    //System.out.println(num);

    while(h!=num.length()){
      v=num.charAt(h)-48;
      double j=1;
      int exp1=num.length();
      for(int f1=0;f1<exp1;f1++){
        j*=.10;
      }
      fnl2+=(h*j);
      j++;
      h++;
    }
    System.out.println(fnl2);
  }
}

The first while loop converts the int part of the string and it works right. But the second while loop should result to the decimal portion of the string. I am having a hard time because double results to huge decimal numbers and it ruins the conversion, and also the second while loop prints the wrong answer.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
user2768260
  • 29
  • 3
  • 6
  • I take it that you're not allowed to use `String.valueOf` or `Double.parseDouble`? – Dennis Meng Sep 13 '13 at 23:20
  • Your method is still a parsing :) – Igor Rodriguez Sep 13 '13 at 23:21
  • I was interested to help you, but your code is difficult to follow, all that d, g, c, h, v... what are that vars for? I will stick to my idea that a good code formatting and a good naming of vars is hundred times time saver for other programmers that want to read your code... – Gianmarco Sep 13 '13 at 23:25
  • What do you mean by "double results to huge decimal numbers"? If you mean that you're getting something like ".003000000001" to the right of the decimal point, that's to be expected because floating-point numbers aren't exact. But there are at least three other errors with your algorithm. I'd suggest working with the program you've started to find the errors, because otherwise you're on the right track. Either use a debugger or put some `System.out.println`'s in the code to see how you're building the result. – ajb Sep 13 '13 at 23:59

3 Answers3

1
int exp1=num.length();

This line is part of your problem. Your loop will always loops num.length() times, but num isn't changing in length (so always 3, in your case). This is causing all three numbers to be treated as thousandths.

Also, remember that decimal values can't be represented exactly in the IEEE-754 format. By doing all these multiplications and additions, you're introducing error into your result. Double.Parse is going to give you the best approximation possible for your number.

Tyler Lee
  • 2,736
  • 13
  • 24
0

You can do the following

String s="124023211.123";
int i;
double result = 0.0f, result2 = 0.0f;
for (i = 0; i < s.length(); i++)
  if (s.charAt(i) == '.')
    break;
  else
    result = result * 10 + (s.charAt(i) - '0');

for (i = s.length()-1 ; i>=0 ; i--)
  if (s.charAt(i) == '.')
    break;
  else
    result2 = result2 / 10 + (s.charAt(i) - '0');

if (i>=0)
  result += result2/10;

Tested for: 11, 3.0, 6.00000, 0005.000, .12, and 124023211.123 Of course, you will not get always the exact value, because sometimes it will be written as an expression as double, like the number you provided. And there are some numbers that have no representation in the binary system.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • BTW...unless I'm missing something, isn't there something, uh, missing from your code? Is this supposed to be Java or Python? ;) – Adi Inbar Sep 14 '13 at 00:08
  • It is Java and tested! – hasan Sep 14 '13 at 00:08
  • @user2768260 I was poking fun at the lack of curly braces. Hey, Jon Skeet says you should always use curly braces, and he's considered a god around here: http://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit-curly-braces-in-java You wouldn't argue with Jon Skeet, would you? :) – Adi Inbar Sep 14 '13 at 00:33
  • Again please give me samples. its only not working for negative numbers. if you want it to work for negative numbers I can edit it for you. – hasan Sep 14 '13 at 00:48
0

Try this one it is working fine tested for valid data

public class Sample1 {
    public static void main(String args[]){
        String num="122312312.2331231";
        String s1 = num.substring(0,num.indexOf("."));
        String s2 = num.substring(num.indexOf(".") + 1,num.length());
        System.out.println(s1);
        System.out.println(s2);
        double n1 = 0;
        double n2 = 0;
        for(int i=0;i<s1.length();i++){
            double d = s1.charAt(i) - '0';
            n1 = n1*10;
            n1 += d;
        }
        System.out.println(n1);
        for(int i=0;i<s2.length();i++){
            double d = s2.charAt(i) - '0';
            //n2 = n2/;
            n2 += d/number(i+1);
        }
        System.out.println(n1+n2);
    }
    public static long number(int n2){
        long d = 10l;
        if(n2>1)
        d =  10 * number(n2-1);
        return d;
    }
}

Gautam Sonar

Gautam
  • 1