在点或逗号之前添加零 [英] Add Zero before point or comma

查看:55
本文介绍了在点或逗号之前添加零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我使用TextFieldParser来读取.TXT文件。



这个是代码:



Hi everyone,

I used a TextFieldParser to read a .TXT file.

This is code:

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

                    reader.HasFieldsEnclosedInQuotes = true;

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







营地价格是格式化的奇怪的模式。在实际价格之前有一些0.例如输出是:



00000000004.50



所以,我用这段代码来调整价格。



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



但是,我有一个以0开头的价格问题,因为从我的字符串中删除了所有零。



例如:

输入:0000000000.80



使用代码:string price = campi [14] .TrimStart(new Char [] {'0 '});



输出:.80



我的程序崩溃了。在这种情况下,我想在点之前添加一个0。



如何解决?



最好的问候。




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.

推荐答案

解析它:

Parse it:
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.
   ...
   }

如果可以,请使用double值而不是字符串,并查看数据库中的定义 - 您不应将值存储为字符串(或者你需要更好的方法来检索值,如果你用数字存储它们)





谢谢你的支持回答。使用你的代码,输出不正确。



例如,我对campi [14]的第一个数据是0000000000000004.97。



如果我使用你的代码,变量d变为497.0和s497.



我的输出必须为4.97。 br />


而且,如果某个价格从0开始像这样的000000000000000.97,我想成为0.97




除了......你没有费心去尝试,是吗?

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?

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...


这篇关于在点或逗号之前添加零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆