LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2004, 02:36 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Rep: Reputation: 47
C# trouble converting from string to double.


public static String[] ParsePowers(String[] InString)
{
String[] OutString = InString;

char[] TestMe;
int[] counter = new int[10];
counter[0] = 0;
while (counter[0] < (InString.Length - 1))
{
TestMe = OutString[(counter[0] + 1)].ToCharArray();
Console.WriteLine("{0}", TestMe[0]);
if (TestMe[0] == '^')
{
OutString = ReString(OutString, counter[0], (counter[0] + 2), DoPower(OutString[counter[0]], OutString[(counter[0] + 2)]));
}
counter[0]++;
}

return OutString;
}

public static string DoPower(String One, String Two)
{
double DOne = System.Convert.ToDouble(One);
double DTwo = System.Convert.ToDouble(Two);
double Out = DOne;
int counter = 0;

while (counter < DTwo)
{
Out = Out * DOne;
counter++;
}

String OutS = Out.ToString();
return OutS;
}

with this tester:

public class Tester
{
static void Main()
{
// public static String[] ParseOne(String InString)
String UHG = "2.2^2.2";

String[] Blug;

Blug = StringMath.ParseOne(UHG);

Blug = StringMath.ParsePowers(Blug);

int counter = 0;
while (counter < Blug.Length)
{
Console.WriteLine("{0}", Blug[counter]);
counter++;
}
}
}

note: ParseOne output the string 2.2^2.2 to a strign array with 2.2 in index 0, ^ in index 1, and 2.2 in index 2.

error:

Uncaught exception: System.FormatException: The supplied value did not have the correct format
at System.Private.NumberParser.ParseDouble(String, NumberStyles, NumberFormatInfo) in ./System/Private/NumberParser.cs:1219
at System.Double.Parse(String, NumberStyles, IFormatProvider) in ./System/Double.cs:127
at System.Double.Parse(String) in ./System/Double.cs:149
at System.Convert.ToDouble(String) in ./System/Convert.cs:2420
at StringMathNS.StringMath.DoPower(String, String)
at StringMathNS.StringMath.ParsePowers(String[])
at Tester.Main()
I tried a writeline and found the bug occurs in the parse to double section (duh, but ti double confirmed it)

what woudl the problem likely be? the ParseOne function works fine.

Last edited by exodist; 02-23-2004 at 02:38 PM.
 
Old 02-23-2004, 10:36 PM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
I dont know what the prblem is but each type (like double) has a method names parse() which converts the given string to its type.
Hope it helps!
 
Old 02-23-2004, 11:38 PM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
Thank you for the help, I ended up writing my own functions/methods to change a string into a double :-D here it is for those who want to share the code:
First you need to convert the String into a char array, after that it is easy to just pump it in (you can convert the string to a char array with: char[] CA = StringVar.ToCharArray() )


public static double MakeDouble(char[] InChar)
{
double Temp = 0, Temp2 = 0;
int[] counter = new int[10];
char TestChar;
bool NotAfterD = true;
double[] Casting = new double[2];
counter[0] = 0;
counter[1] = 0;
counter[2] = 1;

while (counter[0] < 10)
{
while (counter[1] < 10)
{
TestChar = MakeChar(counter[1]);
if ((InChar[counter[0]] == TestChar) && (NotAfterD))
{
Temp = (Temp * 10) + counter[1];
}
if ((InChar[counter[0]] == TestChar) && (!(NotAfterD)))
{
Casting[0] = counter[1];
Casting[1] = counter[2];

Temp2 = (Casting[0]/(Casting[1] * 10));

Temp = Temp + Temp2;
counter[2] = counter[2] * 10;
}
if (InChar[counter[0]] == '.')
{
NotAfterD = false;
}
counter[1]++;
}
counter[0]++;
counter[1] = 0;
}
return Temp;
}

public static char MakeChar(int InNum)
{
char OutChar;
if (InNum == 0)
{
OutChar = '0';
}
if (InNum == 1)
{
OutChar = '1';
}
if (InNum == 2)
{
OutChar = '2';
}
if (InNum == 3)
{
OutChar = '3';
}
if (InNum == 4)
{
OutChar = '4';
}
if (InNum == 5)
{
OutChar = '5';
}
if (InNum == 6)
{
OutChar = '6';
}
if (InNum == 7)
{
OutChar = '7';
}
if (InNum == 8)
{
OutChar = '8';
}
if (InNum == 9)
{
OutChar = '9';
}
return OutChar;
}
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
string to double cynthia Programming 4 10-12-2004 02:40 AM
converting string to asterisks pantera Programming 2 09-13-2004 01:27 PM
Converting a string from /etc/passwd liguorir Linux - Software 3 04-13-2004 02:32 PM
Float/Double to String? Mega Man X Programming 16 01-03-2004 07:50 PM
java covert double into string iceman47 Programming 10 05-28-2003 10:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration