找到你要的答案

Q:String.Format for Integer is Incorrect in VB.Net

Q:字符串。在VB格式的整数是不正确的。网

This should not happen, so I must be missing something simple.

In the below VB function, I am trying to generate a list of part numbers to display on the screen using this format statement:

ticket = String.Format("{0:000}-{1:00000}-{2:00}", storeNumber, order, release)

With that, ticket should have the format xxx-yyyyy-zz so that the ticket is human readable and the other parts of my application can parse this data.

Public Shared Function GetShipTickets(storeNumber As Integer, auditor As String, startDate As DateTime) As ShipTickets
    Dim list As New ShipTickets(auditor)
    list.Display = String.Format("Since {0:MMMM d}.", startDate)
    Const sqlCmd As String =
        "SELECT TICKET_STORE, TICKET_ORDER, TICKET_RELEASE " &
        "FROM TBLRELHDR " &
        "WHERE TICKET_STORE=@TICKET_STORE AND STATUS='C' AND @CREATE_DATE<=CREATE_DATE " &
        "ORDER BY TICKET_ORDER, TICKET_RELEASE, CREATE_DATE; "
    Dim table As New DataTable()
    Using cmd As New DB2Command(sqlCmd, Db2CusDta)
        cmd.Parameters.Add("TICKET_STORE", DB2Type.SmallInt).Value = storeNumber
        cmd.Parameters.Add("CREATE_DATE", DB2Type.Char, 8).Value = String.Format("{0:yyyyMMdd}", startDate)
        table.Load(cmd.ExecuteReader())
    End Using
    If 0 < table.Rows.Count Then
        For Each row As DataRow In table.Rows
            Dim order As String = String.Format("{0}", row("TICKET_ORDER")).Trim().ToUpper()
            Dim release As String = String.Format("{0}", row("TICKET_RELEASE")).Trim().ToUpper()
            Dim ticket As String = String.Format("{0:000}-{1:00000}-{2:00}", storeNumber, order, release)
            list.Add(ticket)
        Next
    End If
    list.Sort()
    Return list
End Function

It is not working, though.

Also, when I view my data on the screen, it is not displaying correctly either:

What is going on?

VB is not my strongest programming language. Either there is some nuance of VB that I am unaware of or the compiler is messing up.

Using Visual Studio 2012

这不应该发生,所以我必须错过一些简单的东西。

在下面的VB函数,我试图生成一个在使用该语句格式的屏幕显示部分数据的列表:

ticket = String.Format("{0:000}-{1:00000}-{2:00}", storeNumber, order, release)

这样,票应该有格式xxx- yyyyy -zz,门票是可读的,我的应用程序的其他部分可以分析此数据。

Public Shared Function GetShipTickets(storeNumber As Integer, auditor As String, startDate As DateTime) As ShipTickets
    Dim list As New ShipTickets(auditor)
    list.Display = String.Format("Since {0:MMMM d}.", startDate)
    Const sqlCmd As String =
        "SELECT TICKET_STORE, TICKET_ORDER, TICKET_RELEASE " &
        "FROM TBLRELHDR " &
        "WHERE TICKET_STORE=@TICKET_STORE AND STATUS='C' AND @CREATE_DATE<=CREATE_DATE " &
        "ORDER BY TICKET_ORDER, TICKET_RELEASE, CREATE_DATE; "
    Dim table As New DataTable()
    Using cmd As New DB2Command(sqlCmd, Db2CusDta)
        cmd.Parameters.Add("TICKET_STORE", DB2Type.SmallInt).Value = storeNumber
        cmd.Parameters.Add("CREATE_DATE", DB2Type.Char, 8).Value = String.Format("{0:yyyyMMdd}", startDate)
        table.Load(cmd.ExecuteReader())
    End Using
    If 0 < table.Rows.Count Then
        For Each row As DataRow In table.Rows
            Dim order As String = String.Format("{0}", row("TICKET_ORDER")).Trim().ToUpper()
            Dim release As String = String.Format("{0}", row("TICKET_RELEASE")).Trim().ToUpper()
            Dim ticket As String = String.Format("{0:000}-{1:00000}-{2:00}", storeNumber, order, release)
            list.Add(ticket)
        Next
    End If
    list.Sort()
    Return list
End Function

它不是工作,虽然。

此外,当我查看我的数据在屏幕上,它是不正确显示:

What is going on?

VB不是我最强的编程语言。或者是有一些细微的VB,我不知道或编译器是搞乱了。

使用Visual Studio 2012

answer1: 回答1:

The format "{2:00}" works on integers, not strings. It won't automatically convert a string consisting of digits into an integer. Manually convert the strings to integers:

Dim ticket As String = String.Format("{0:000}-{1:00000}-{2:00}", 
                                     CInt(storeNumber), 
                                     CInt(order), 
                                     CInt(release))

格式“{ 2 }”工程的整数,而不是字符串。它不会自动将数字组成的字符串转换为整数。手动将字符串转换为整数:

Dim ticket As String = String.Format("{0:000}-{1:00000}-{2:00}", 
                                     CInt(storeNumber), 
                                     CInt(order), 
                                     CInt(release))
vb.net  visual-studio-2012  string-formatting