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