Home > Back-end >  how to change NAN to 0 in vb.net
how to change NAN to 0 in vb.net

Time:01-28

I want to change the NAN result in the text label to 0.

is there a solution in the recommendation?.

Thanks

Private Sub Calculatedispercent()
            Dim DisTotal As Double
            Dim Total As Double
            Double.TryParse(Lbldistotal.Text, DisTotal)
            Double.TryParse(lblTotal.Text, Total)
 lblDispercent.Text = (DisTotal / Total * 100).ToString("N2")
End Sub
Private Sub CalculateGrandTotal()
            Dim tot As Double = 0
            Dim Dispercent As Double
Double.TryParse(lblDispercent.Text, Dispercent)
 For Each item As DataGridViewRow In grid.Rows
                Dim val As Double
                Double.TryParse(CType(item.Cells(5).Value, String), val)
                tot  = val
            Next item
            lblGrandTotal.Text = (tot * (1 - Dispercent / 100)).ToString("N2")
End Sub

RESULT NAN

CodePudding user response:

Evaluate the result of TryParse and use an If like here:

Private Sub Calculatedispercent()
    Dim DisTotal As Double
    Dim Total As Double
    Dim result As Double = 0
    If Double.TryParse(Lbldistotal.Text, DisTotal) AndAlso
       Double.TryParse(lblTotal.Text, Total) Then
        result = If(Total = 0, 0, DisTotal / Total * 100)
    End If
    lblDispercent.Text = result.ToString("N2")
End Sub
  • Related