string 我怎样才能在任何数字前面得到0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3102808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i get 0 in front of any number?
提问by KoolKabin
I would like to append 0 before a number if it is single digit. For example it should be 01,02,03... 09, 10, 11, ...
如果数字是个位数,我想在数字前附加 0。例如它应该是 01,02,03... 09, 10, 11, ...
回答by Tim Schmelter
Dim yourNumber as Int32 = 5
yourNumber.ToString("D2") '= "05"
回答by jvenema
Try this:
尝试这个:
myNum.ToString().PadLeft(2, "0");
回答by Lance
Try the following...
尝试以下...
Dim varNumber As Integer = 3
Dim number As String = String.Format("{0:0#}", varNumber)
Hope that helps.
希望有帮助。
回答by MarkJ
Old school method from VB6, still works:
来自 VB6 的老派方法,仍然有效:
Dim yourNumber as Long = 5
Format(yourNumber, "00") ' = "05" '
... just for old time's sake :). Better to use Tim's answer.
...只是为了旧时光:)。最好使用蒂姆的回答。
回答by Mattias
if(number < 10){
number = Int32.Parse("0" + number.ToString());
}
I guess that was some c# going on :) but you should get the idea.
我想这是一些 c# 正在进行 :) 但你应该明白这个想法。