简体   繁体   English

将任何数字格式化为三位数?

[英]Format any number to three digits?

I want to insert any number was entered by the user, to the database like a three digits.我想将用户输入的任何数字插入到数据库中,就像三位数一样。

eg if the user insert ( 1 ) i want to save the number as ( 001 ), or ( 20 ) to ( 020 ).例如,如果用户插入 ( 1 ) 我想将数字保存为 ( 001 ),或 ( 20 ) 到 ( 020 )。

note: The data type of the database column is string not integer.注意:数据库列的数据类型是字符串而不是整数。

int iVal = 1;

iVal.ToString("D3"); // = "001"

Read more on MSDNMSDN上阅读更多内容

You can try你可以试试

 int value =5;
 Console.WriteLine(value.ToString("000"));
 Console.WriteLine(value.ToString().PadLeft(3,'0'));

produces:产生:

005 005

005 005

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM