Simple Math Opertation

StewartBW 545 Reputation points
2024-05-13T13:12:52.5666667+00:00

Hello

I need to calculate how many parts an SMS text is, the text is US Ascii so here's the long code with minimum of 15 lines:

.Length > 0 AndAlso .Length <= 160 Then Return 1
.Length > 160 AndAlso .Length <= 320 Then Return 2
.Length > 320 AndAlso .Length <= 480 Then Return 3
.Length > 480 AndAlso .Length <= 640 Then Return 4
.Length > 640 AndAlso .Length <= 800 Then Return 5
...
.Length > 2240 AndAlso .Length <= 2400 Then Return 15

I'm not familiar with .net math methods, just checking if there's a simpler way to do this calculation?

Thanks for help.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,608 questions
{count} votes

Accepted answer
  1. Dewayne Basnett 1,361 Reputation points
    2024-05-13T17:15:14.3366667+00:00

    Here is a method that gives the results specified.

    Private Function SomeMethod(SMSText As String) As Integer
    
        Dim rv As Integer = CInt(Math.Ceiling(SMSText.Length / 160))
    
        Return rv
    
    End Function
    

    And some test code

        Dim s As String
    
        For x As Integer = 1 To 16
    
            s = New String("*"c, (x * 160) - 1)
    
            Debug.Write(s.Length)
    
            Debug.Write("    ")
    
            Debug.WriteLine(SomeMethod(s))
    
            s &= "*"
    
            Debug.Write(s.Length)
    
            Debug.Write("    ")
    
            Debug.WriteLine(SomeMethod(s))
    
            s &= "*"
    
            Debug.Write(s.Length)
    
            Debug.Write("    ")
    
            Debug.WriteLine(SomeMethod(s))
    
            Debug.WriteLine("")
    
        Next
    
    1 person found this answer helpful.
    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Viorel 113.2K Reputation points
    2024-05-13T13:42:12.82+00:00

    Try something like this:

    Dim d = .Length \ 160
    Dim r = .Length Mod 160
    
    Return If(r <> 0, d + 1, d)
    
    0 comments No comments

  2. Jiachen Li-MSFT 27,241 Reputation points Microsoft Vendor
    2024-05-14T01:34:45.3266667+00:00

    Hi @StewartBW ,

    Use Mod operator to determine if it is an integer multiple of 160, then use / to determine the multiple.

        Public Function count(str As String) As Integer
            If (str.Length Mod 160) = 0 Then
                Return str.Length / 160
            Else
                Return str.Length / 160 + 1
            End If
        End Function
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. KOZ6.0 5,295 Reputation points
    2024-05-14T01:56:20.05+00:00

    If A and B are both positive integers, the following formula holds.

    ↑A/B↑ = ↓(A+B-1)/B↓

    Function GetLineCount(length As Integer) As Integer
        Return (length + 159) \ 160
    End Function
    
    0 comments No comments