Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi everyone,

I used a TextFieldParser to read a .TXT file.

This is code:

C#
using (TextFieldParser reader = new TextFieldParser(txtPath.Text))
{
                    reader.SetDelimiters(new string[] {"\t"});

                    reader.HasFieldsEnclosedInQuotes = true;

                    while (!reader.EndOfData)
                    {
                        string[] campi = reader.ReadFields();
                    }
}



The camp "price" is formatting in a strange mode. Before the real price there are some 0. For example an output is:

00000000004.50

So, I use this code to adjust the price.

string price = campi[14].TrimStart(new Char[] { '0' });

But, I have a problem with price that start with 0, because all zero were removed from my string.

For example:
Input: 0000000000.80

Using code: string price= campi[14].TrimStart(new Char[] { '0' });

Output: .80

And my program crash. In this case, I want to add a 0 before the point.

How can I solve ?

Best Regards.
Posted
Comments
Leo Chapiro 30-Oct-15 4:40am    
Please consider to mark solution1 as accepted. Why ask a question and not accept a solution that helps?

1 solution

Parse it:
C#
double d;
if (double.TryParse(campi[14], out d))
   {
   // d contains your value as a numeric value, regardless of the number of leading zeros
   string s = d.ToString();
   // s contains the number as a "regular" double: 0.12, 4.67, etc.
   ...
   }
If you can, use the double value instead of the string, and look at the definitions in your DB - you shouldn't be storing values as strings (or you need a better way of retrieving values if you are storing them numerically)


"Thank you for your answer. Using your code, output isn't correct.

For example, my first data for campi[14] is 0000000000000004.97.

If i using your code the variable "d" become 497.0 and "s" 497.

My output must to is 4.97.

And, if some price start with 0 like this 000000000000000.97 , I want to became 0.97"


Except...you didn't bother to try it, did you?
C#
string[] campi = new string[15];
double d;
campi[14] = "0000000000000004.97";
if (double.TryParse(campi[14], out d))
    {
    // d contains your value as a numeric value, regardless of the number of leading zeros
    string s = d.ToString();
    // s contains the number as a "regular" double: 0.12, 4.67, etc.
    Console.WriteLine("{0}:{1}", d, s);
    }
campi[14] = "000000000000000.97";
if (double.TryParse(campi[14], out d))
    {
    // d contains your value as a numeric value, regardless of the number of leading zeros
    string s = d.ToString();
    // s contains the number as a "regular" double: 0.12, 4.67, etc.
    Console.WriteLine("{0}:{1}", d, s);
    }

Gives the output:
4.97:4.97
0.97:0.97

Which is exactly what you asked for...
 
Share this answer
 
v2
Comments
Member 10558090 29-Oct-15 6:02am    
Thank you for your answer. Using your code, output isn't correct.

For example, my first data for campi[14] is 0000000000000004.97.

If i using your code the variable "d" become 497.0 and "s" 497.

My output must to is 4.97.

And, if some price start with 0 like this 000000000000000.97 , I want to became 0.97
OriginalGriff 29-Oct-15 6:21am    
Answer updated - check your data!
Matt T Heffron 29-Oct-15 12:48pm    
+5but I'd suggest using decimal instead of double just to be sure there isn't any funny rounding since these are "prices"
Leo Chapiro 30-Oct-15 4:42am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900