C# 格式化来自Web API的响应
我有一个Web API运行一个存储过程并从表中返回记录。该记录包括名为CounterSeq的int字段C# 格式化来自Web API的响应,c#,asp.net-web-api,C#,Asp.net Web Api,我有一个Web API运行一个存储过程并从表中返回记录。该记录包括名为CounterSeq的int字段 [HttpGet] public IHttpActionResult Get(string Account) { // other code for connecting to the SQL seerver and calling the stored procedure reader = command.ExecuteReader(); List<QueryResult>
[HttpGet]
public IHttpActionResult Get(string Account)
{
// other code for connecting to the SQL seerver and calling the stored procedure
reader = command.ExecuteReader();
List<QueryResult>qresults = new List<QueryResult>();
while (reader.Read())
{
QueryResult qr = new QueryResult();
qr.AccountID = reader["AccountID"].ToString();
qr.CounterSeq = reader["CounterSeq"].ToString();
qresults.Add(qr);
}
DbConnection.Close();
return Ok(qresults);
但是,即使是两位数字,它也显示为“0012”,但它必须是“012”,我如何处理这个问题
qr.CounterSeq=读卡器[“CounterSeq”]。ToString(“0000”)代码>您可以尝试以下方法:
qr.CounterSeq = reader.GetInt32(reader.GetOrdinal("CounterSeq")).ToString("D4");
下面是带有1个参数的ToString错误的答案:
您可以在此处阅读有关十进制(“D”)格式说明符的更多信息:
您可以使用
返回指定长度的新字符串,其中当前字符串的开头用空格或指定的Unicode字符填充
它抛出错误1“ToString”方法的无重载1参数您使用的是哪个版本的visual studio?我有visual studio 2013
qr.CounterSeq = reader.GetInt32(reader.GetOrdinal("CounterSeq")).ToString("D4");
qr.CounterSeq = reader["CounterSeq"].ToString().PadLeft(3, '0');