With A Percentage Sign After Numeric Numbers is preventing me to calculate sums

Gary Simpson 471 Reputation points
2023-02-17T22:58:20.68+00:00

Percentage Sign After Numeric Numbers I have in Different Labels,

Example of Label values, Label1 ( 0%) , Label2 (13.25%), Label3 (3.25%)

When I try to get a value from one of the above Labels, And times One of the above Labels with

Another Label ( LbTaxRate ) Then I want to put a value into a textbox. But Nothing seems to happen or I get thrown an Error.

My Question is How do I remove the Percent sign before doing the sum. Code I have at the Moment is below... plus image of my table of Labels


 If LbPayPeriod.Text = "Weekly" AndAlso LbTaxLetter.Text = "L" And GetDec(TxtAmountEarned.Text >= GetDec(TxtHeader2WFrom.Text) AndAlso

          GetDec(TxtAmountEarned.Text <= GetDec(TxtHeader2WTo.Text))) Then



            Dim Sum3 As Decimal = GetPercent(LbB2.Text) * GetDec(TxtTaxableIncome.Text) / 100

            Dim Sum4 As Decimal = GetPercent(LbL2.Text)



            TxtN_I_Deductions.Text = FormatCurrency(Sum3)



        End If



    End Sub


    Function GetPercent(s As String) As Decimal

        If s.EndsWith("%") Then

            s = s.Substring(1, s.Length - 1)

        End If

        Dim v As Decimal = 0D

        If Decimal.TryParse(s, v) Then Return v

        Return 0D

    End Function

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

Accepted answer
  1. LesHay 7,136 Reputation points
    2023-02-17T23:20:36.1633333+00:00

    Hi

    Here is one way to do it.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        Dim s As String = "22.55%"
    
        ' here, a valid Decimal numeric value
        ' is available (0 if string was not
        ' a valid Decimal)
        Dim dec As Decimal = GetPercent(s)
    
        Stop
    
      End Sub
      Function GetPercent(s As String) As Decimal
        Dim ss As String = s.Replace("%"c, String.Empty)
        Dim v As Decimal = 0.0D
        If Decimal.TryParse(ss, v) Then
          Return v
        End If
        ' if the string can not be converted to
        ' a valid Decimal then it will return 0
        Return 0.0D
      End Function
    End Class
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. LesHay 7,136 Reputation points
    2023-02-18T17:36:14.5133333+00:00

    Hi

    Here is a very small stand alone example where textboxes contain either a trailing % or currency symbol. The example caters for numeric conversion of either to use in calculating a result. The advantage of something like this is that any number of textboxes/labels in appropriate groups can be looped through with minimal code.

    The example uses local currency symbol and a trailing % (NOTE: not a % function which multiplies number by 100).

    If you choose to try this out, start a new test project and copy/paste the code (the Form is blank)

    Option Strict On
    Option Explicit On
    Imports System.Globalization
    Public Class Form1
      Dim WithEvents tb1, tb2 As New TextBox
      Dim lab1 As New Label
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 2 textboxes and 1 label for demo
        With tb1
          .Text = 22.55.ToString("#.##\%")
          ' OR    .Text = "22.55%"
          .Location = New Point(10, 20)
          .TextAlign = HorizontalAlignment.Right
          .AutoSize = True
          .Font = New Font(.Font.FontFamily, 16)
        End With
        With tb2
          .Text = 123.6.ToString("C2")
          ' OR    .Text = "£123.6" local currency symbol
          .Location = New Point(140, 20)
          .TextAlign = HorizontalAlignment.Right
          .AutoSize = True
          .Font = New Font(.Font.FontFamily, 16)
        End With
        With lab1
          .Location = New Point(280, 20)
          .TextAlign = ContentAlignment.MiddleRight
          .AutoSize = False
          .BorderStyle = BorderStyle.FixedSingle
          .Size = New Size(120, 30)
          .Font = New Font(.Font.FontFamily, 16)
        End With
        Controls.AddRange({tb1, tb2, lab1})
        Calc()
      End Sub
      Private Sub tb_Validated(sender As Object, e As EventArgs) Handles tb1.Validated, tb2.Validated
        Calc()
      End Sub
      Sub Calc()
        tb1.Text = GetDec(tb1.Text).ToString("#.##\%")
        lab1.Text = (GetDec(tb1.Text) * GetDec(tb2.Text) / 100).ToString("C2")
    
        tb2.Text = GetDec(tb2.Text).ToString("C2")
        lab1.Text = (GetDec(tb1.Text) * GetDec(tb2.Text) / 100).ToString("C2")
      End Sub
      Function GetDec(s As String) As Decimal
        Dim ss As String = s.Replace("%"c, String.Empty)
        Dim v As Decimal = 0.0D
        If Decimal.TryParse(ss,
                        NumberStyles.Any, CultureInfo.CurrentCulture, v) Then
          Return v
        End If
        Return 0.0D
      End Function
    End Class
    
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.