开发者

Reading initial 0 of number in data grid view - C#.NET

I am trying to read data from the text file. My text file consist of data, say, 002200;20.

I am displaying those value in data grid view. Every time, I am displaying data from text file it reads only 2200;20. It doesn't read initial 0 any time.

I want to read 002200 instead of 2200.

I am using double as a data type for that.

Can anyone help me for that?开发者_JAVA技巧

Thanks, Rushabh Shah.


Numbers do not have leading zeroes.

If you want to always have leading zeroes, you should set the grid to use a format string.

If you want to preserve leading zeroes from the file, you should read strings, not numbers.


If all you are doing is displaying a value straight from a text file, declare the variable as a string instead of a double. Then it will display as-is in the Grid View.


You have two approaches you can take with this:

  1. You can read the data in as a string value, and then convert it to a double whenever you need to do calculations with it.

  2. You can continue to read the data in as a double, but when you go to display it, you can use the following code:

    MyDoubleValue.ToString().PadLeft(6, "0"c);

This will put 0 characters to the left of it until you reach 6 characters total in length. You can adjust this length based on your input value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜