我需要在一个文本框中保持前导零,用于将数据输入到数据库中。它用于SKU编号。我需要能够键入前导零(例如001234567890),然后将它们输入数据库。
但是,我想不出如何将前导零保持为双精度。我不需要字符串格式,而是双精度格式。有没有办法在保留数字而不是字符串的同时做到这一点?
例如。
txtProductBarcode.Text = Format(txtProductBarcode.Text, "############")
'This does not work发布于 2012-09-26 08:08:52
使用0代替#将导致输出前导零。
例如:
Dim s as string = string.Format("{0:000###}", 12345d)
Console.WriteLine(s)将输出
012345请参阅MSDN上的Pad a Number with Leading Zeros。
顺便说一句,你不确定为什么你会担心保留前导零,而同时“作为一个双精度”。双精度值只是一个双精度值,显示格式应该无关紧要。0123与123相同,否则应使用字符串数据类型,而不是双精度类型。
发布于 2018-05-26 13:50:12
试试看这就是其中之一。您可以将整数num转换为双精度。
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim num As Integer
Dim temp As String = ""
Try
If Regex.IsMatch(TextBox1.Text, "^[0-9 ]+$") Then
num = Integer.Parse(TextBox1.Text)
If num <> 0 then
TextBox1.Text = num.ToString("D6")
temp = num
else
TextBox1.ReadOnly = False
End If
If num.Length = 6 Then
TextBox1.ReadOnly = True
TextBox1.BackColor = Color.White
ElseIf num = 0 Then
TextBox1.ReadOnly = False
End If
TextBox1.SelectionStart = TextBox1.Text.Length
End If
Catch ex As Exception
Msgbox(ex.Message)
End Try
End Subhttps://stackoverflow.com/questions/12592739
复制相似问题