Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用前导零'格式化字符串;s_C#_Formatting - Fatal编程技术网

C# 使用前导零'格式化字符串;s

C# 使用前导零'格式化字符串;s,c#,formatting,C#,Formatting,我正在尝试将一个对象(来自SQL server)转换为一个整数,这样我就可以格式化该数字,使其前面有正确数量的零 例如: 如果我有25.6,我需要它是0025.6 现在我在网上看到了如何做到这一点,但我看到人们发布的方法对我并不适用。我不完全清楚为什么。我正在尝试格式化GlobalVariables.grossWighter。我从SQL server读取值GlobalVariables.grossweight,但当我TryParse它时,它会丢失它的值。我的代码如下: w

我正在尝试将一个对象(来自SQL server)转换为一个整数,这样我就可以格式化该数字,使其前面有正确数量的零

例如:

如果我有25.6,我需要它是0025.6

现在我在网上看到了如何做到这一点,但我看到人们发布的方法对我并不适用。我不完全清楚为什么。我正在尝试格式化
GlobalVariables.grossWighter
。我从SQL server读取值
GlobalVariables.grossweight
,但当我
TryParse
它时,它会丢失它的值。我的代码如下:

            while (TransferRecord.Read())
            {
                //Pulling data from the SQL server. getting data for every line of code as specified.
                GlobalVariables.baledate = TransferRecord["keyprinter_datetime"];
                GlobalVariables.baleline = TransferRecord["pulp_line_id"];
                GlobalVariables.baleid = TransferRecord["bale_id"];
                GlobalVariables.grossweight = TransferRecord["bale_gross_weight"];
                GlobalVariables.grossweightflag = TransferRecord["gross_value_flag"];
                GlobalVariables.baleairdrypercent = TransferRecord["bale_airdry_pct"];
                GlobalVariables.airdryflag = TransferRecord["airdry_value_flag"];

                //Converting the date, and the baleid to fit in the string.
                DateTime.TryParse(GlobalVariables.baledate.ToString(), out GlobalVariables.baledateafter);
                int.TryParse(GlobalVariables.baleid.ToString(), out GlobalVariables.baleidafter);

                int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter);
                GlobalVariables.grossweightafter.ToString("0000.0");
                //Calling the WriteData method.
                WriteData();
            }

因此,我想知道是否有人能发现我做错了什么,或者他们能帮助我找到正确的方法来解决这个问题。

Hans Passant说的是,您需要分配从.ToString返回的值。这条线应该是:

GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");

@Hans Passant的意思是,您需要分配从.ToString返回的值。这条线应该是:

GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");

最后一行应该是

if(int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter))
{
    string grossWeightAfter = GlobalVariables.grossweightafter.ToString("0000.0");
    //you need to save the string returned from the ToString-method somewhere or it will be lost.
    ///Alternatively, if GlobalVariables can contain strings aswell:
    GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
}
else
{
    //React on value not being an int
}

最后一行应该是

if(int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter))
{
    string grossWeightAfter = GlobalVariables.grossweightafter.ToString("0000.0");
    //you need to save the string returned from the ToString-method somewhere or it will be lost.
    ///Alternatively, if GlobalVariables can contain strings aswell:
    GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
}
else
{
    //React on value not being an int
}

也许您应该尝试使用
double.TryParse()
方法而不是
int.TryParse()
,因为int没有小数部分

此外,还需要将
ToString()
结果存储到字符串变量中。您的代码应该如下所示:

GlobalVariables.grossweightafterstring = GlobalVariables.grossweightafter.ToString("0000.0");

也许您应该尝试使用
double.TryParse()
方法而不是
int.TryParse()
,因为int没有小数部分

此外,还需要将
ToString()
结果存储到字符串变量中。您的代码应该如下所示:

GlobalVariables.grossweightafterstring = GlobalVariables.grossweightafter.ToString("0000.0");

建议使用ToString()的返回值。就像你永远不应该忽略TryParse()的返回值一样。你是说我应该使用
GlobalVariables.grossWightAfter.ToString()
?稍微扩展一下@HansPassant所说的:当你使用
TryParse()
时,你应该根据值是否可解析来检查返回值和分支。例如,如果无法将值解析为整数,则将该值设置为“Unrecognized”。如果您想不检查就快速完成,只需使用
字符串myVar=Int32.Parse(rawValue)
。最后如果值应该是十进制的,为什么要解析为
int
?我没有想到int,我把它改成了double,并按照ps2goat的建议做了。我找到了解决办法。谢谢。建议使用ToString()的返回值。就像你永远不应该忽略TryParse()的返回值一样。你是说我应该使用
GlobalVariables.grossWightAfter.ToString()
?稍微扩展一下@HansPassant所说的:当你使用
TryParse()
时,你应该根据值是否可解析来检查返回值和分支。例如,如果无法将值解析为整数,则将该值设置为“Unrecognized”。如果您想不检查就快速完成,只需使用
字符串myVar=Int32.Parse(rawValue)
。最后如果值应该是十进制的,为什么要解析为
int
?我没有想到int,我把它改成了double,并按照ps2goat的建议做了。我找到了解决办法。非常感谢。