Jump to content

char [] to double?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Nabusman

Nabusman

    Newbie

  • Members
  • PipPip
  • 18 posts
I'm trying to take some numbers from a text file (eg. 34.54,23.43,45.98) and run some basic formulas on them. I'm reading the text file using Streamreader's Read() function which puts the data into a character array. My question is how do i get this data into a double format so that i can actually use it in the program?

Thanks.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
I'm not sure it is possible. How would you determine when the number you want starts and ends.

[0] = 3
[1] = 4
[2] = [.]
[3] = 5
[4] = 4
[5] = 2
[6] = 3

How would you know thats 34.54 and not 34.5423? Are all numbers only 4 digits [5 characters]? In that case it wouldn't be that difficult. [Dont know the exact syntax, but can help you with the theory]

#3
Nabusman

Nabusman

    Newbie

  • Members
  • PipPip
  • 18 posts
Right, they are actually all numbers of 6 characters (one before the decimal, the decimal, and 4 numbers after). Plus, there is a comma delimiter, which i don't know if that helps. I was was thinking if it was possible to create a string and put up a for loop that loops through the array converting the char to a string and concatenating it to the string - if that makes any sense...

Of course then it would be possible to split the string using the comma as a delimiter. However, I'd have to use an array of 7 characters to include the comma.

#4
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Yes, that is exactly what I was thinking. If you have control over the text file you are using, it might be better to put each piece of data on its own line. I know a lot of programming languages have a 'readLine' function. That way you could 'get' strings rather than a character array.

But if you must, just create a nested loop. The outer looping through 7 elements, the inner looping through 6 elements. The inner loop will be your concatenated string which you can just cast to a double and use as you please.

I hope this makes sense.

#5
Nabusman

Nabusman

    Newbie

  • Members
  • PipPip
  • 18 posts
Makes perfect sense, don't know why i didn't think of that... I'll put each number on a different line. Thanks.

Nabs

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Doing this is pretty easy.
Let's say you have an array: CharArray[7]

int i;

float myDouble;

float myMult;

bool decimal;

myDouble := 0.0;

myMult := 1.0;

decimal := false;

for (i := 0 ; i++; i<=6)

{

  if CharArray[i] = '.' then

  {

    decimal := true;

  }

  else

  {

    if !decimal then 

      myDouble := myDouble * 10

    else

      myMult := myMult / 10;

    myDouble := myDouble + CharArray[i]*myMult;

  }

};


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Nabusman

Nabusman

    Newbie

  • Members
  • PipPip
  • 18 posts
Thanks for the code WingedPanther, good to know to know how to do this.

Nabs

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Looking at it, you'll have to add a conversion from the character to the digit, but that should be only a minor oversight.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog