如果Id为1,我试图以这种格式0000001生成Ref编号。我有一个前缀F和00000将被ID覆盖在后面。 ID是autoIncrement
这是我的方法但它给F-1等等我想要F-0000001。
public class Function : BaseModel
{
public Function()
{
Ref = Sequence;
}
[ForeignKey("Corporate")]
public int CorporateId { get; set; }
public virtual Corporate Corporate { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Title { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public virtual User User { get; set; }
public bool Approved { get; set; } = false;
public bool Completed { get; set; } = false;
public DateTime DateCreated { get; set; } = DateTime.Now;
public string Note { get; set; }
public string ContactPerson { get; set; }
public string ContactPersonPhone { get; set; }
public string ContactPersonEmail { get; set; }
public string Ref { get; set; }
private string Sequence
{
get
{
var seq = "F" + "-" + Id;
seq = seq.Replace(" ", "000000");
return seq;
}
}
}
答案 0 :(得分:3)
只需将String.Format与custom format string:
一起使用即可String.Format("F-{0:0000000}", 1056)
返回:F-001056
0
是零占位符Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
格式项({0:...}
)之外的文本按原样复制到字符串中。
该属性应如下所示:
private string Sequence { get{ return String.Format("F-{0:0000000}", Id);}}
或
private string Sequence => String.Format("F-{0:0000000}", Id);
答案 1 :(得分:1)
尝试使用C#的PadLeft函数:
private string Sequence
{
get
{
return "F-" + Id.ToString().PadLeft(6,"0");
}
}
答案 2 :(得分:1)
使用Int32.ToString,在您的媒体资源中传递以下格式字符串,如下所示:
private string Sequence => "F-" + Id.ToString("D7");
更改" D7"表示要填充多少个零。