简体   繁体   中英

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 ).

note: The data type of the database column is string not integer.

int iVal = 1;

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

Read more on MSDN

You can try

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

produces:

005

005

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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