Vb.net 格式数字转字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29027148/
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
Vb.net format number to String
提问by le lan
In Visual Basic .NET, I have an integer value:
在 Visual Basic .NET 中,我有一个整数值:
Dim myNum as Integer = 123
How can I convert this to a String as "0123" (have 4 number digits)?
如何将其转换为字符串“0123”(有 4 个数字)?
回答by Joel Coehoorn
It's not clear from your question whether you want 4 characters or 5. Here's how you'd do it for 5. If you want 4, just drop one of the zeros in the format string.
从您的问题中不清楚您想要 4 个字符还是 5 个字符。以下是您如何处理 5 个字符。如果您想要 4 个字符,只需在格式字符串中删除一个零即可。
Dim myNum as Integer = 123
Dim myNumString As String = myNum.ToString("00000")
You can also do this with String.Format()
:
你也可以这样做String.Format()
:
Dim myNumString As String = String.Format("{0:00000}", myNum)
And now string interpolation:
现在字符串插值:
Dim myNumString As String = $"{myNum:00000}"
回答by Ambasabi
As far as converting an integer to a string, try simply using a setup like this:
至于将整数转换为字符串,请尝试简单地使用如下设置:
Dim myNum as Integer = 123;
Dim stringValue as String;
stringValue = CStr(myNum);
If you are needing to add leading zeroes to this, you can either append them with a string manually, or you can try writing a macro that goes something like:
如果您需要为此添加前导零,您可以手动为它们附加一个字符串,或者您可以尝试编写一个类似于以下内容的宏:
If (myNum - (10 * (myNum / 10))) = 5 Then
stringValue = CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 4 Then
stringValue = "0" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 3 Then
stringValue = "00" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 2 Then
stringValue = "000" & CStr(myNum)
ElseIf (myNum - (10 * (myNum / 10))) = 1 Then
stringValue = "0000" & CStr(myNum)
Else
stringValue = "00000"
The (myNum - (10 * (myNum / 10))) is effectively modulus math. Credit: VBA equivalent to Excel's mod function
(myNum - (10 * (myNum / 10))) 是有效的模数数学。信用:VBA 相当于 Excel 的 mod 函数
I did not test this code, so I apologize if it does not work out of the box, but the general idea is above. The above is also assuming numbers like 123, or 1234, etc. You can also try manipulating strings and combining them by testing the length of your newly converted string.
我没有测试这段代码,所以如果它不能开箱即用,我深表歉意,但总体思路如上。上面还假设了 123 或 1234 等数字。您还可以尝试通过测试新转换的字符串的长度来操作字符串并组合它们。
Dim newString as String
If Len(stringValue) = 3 Then
newString = "00" & stringValue
ElseIf Len(stringValue) = 2 Then
newString = "000" & stringValue
etc.
等等。
回答by A-Taher
Here is a very simple way to convert a number to a string and adding leading zeros if the number length is less than 5 digits
这是将数字转换为字符串并在数字长度小于 5 位时添加前导零的一种非常简单的方法
Dim myNum as Integer = 123
Dim strNum as String
strNum = String(5-len(myNum),"0") & cstr(myNum)
debug.print strNum 'This will display 00123
回答by nanda9894
try like Tostring() format.
尝试像 Tostring() 格式。
myNum.ToString("00000")