Pregunta

Good morning. I hate to bother you guys but I encountered with problem which really stumped me.

I have the Excel file where I have 2 columns where I must divided 1 column by another and insert this result into SQL table.

Here is the snapshot of excel file, where I underline the problem row.

enter image description here

If values in both columns have "," like in other rows then everything is fine, but if 1 have "," and another doesn't then I get wrong result. For example the compiler read the 147 like 147.0 {Double} and 60,75 like "60,75"{String}. If everything is fine I should get the result 2,41, but I got 0,024 (it is like 60,75 is converted to 6075). Unfortunately I can't modify Excel file. How can I get the right result?

Here is the code for division:

            Dim usedRange = xlsWorkSheet.Range("E7", "F57")
            Dim usedRangeAs2DArray As Object(,) = usedRange.Value2
            Dim TeamIndex(), import As String
            ReDim TeamIndex(usedRange.Rows.Count)
            For i As Integer = 1 To usedRange.Rows.Count
             If (usedRangeAs2DArray(i, 1) = 0 And usedRangeAs2DArray(i, 2) = 0)
                Or usedRangeAs2DArray(i, 2) = 0 Then
                    z = 1
                    TeamIndex(i) = z
                Else
                    g = Convert.ToString(usedRangeAs2DArray(i, 1))
                    z = g / Convert.ToDouble(usedRangeAs2DArray(i, 2))
                    z = Math.Floor(100 * z) / 100
                    TeamIndex(i) = z
                End If
                Next

I tried to use Convert.ToString(), use Replace "." with "," and String.Format("{0:N2}", g) on first column, but this approaches simply ignored.

Thank you in advance.

¿Fue útil?

Solución

I guess I found not very beautiful, but nonetheless working solution to this problem. The main idea if I have integer value, without "," which is readed by compiler as type {Double}, I simple append ",00" string to this value. Here is my modified code.

    Dim usedRange = xlsWorkSheet.Range("E7", "F57")
    Dim usedRangeAs2DArray As Object(,) = usedRange.Value2
    Dim TeamIndex(), import As String
    ReDim TeamIndex(usedRange.Rows.Count)
    For i As Integer = 1 To usedRange.Rows.Count
     If (usedRangeAs2DArray(i, 1) = 0 And usedRangeAs2DArray(i, 2) = 0)
        Or usedRangeAs2DArray(i, 2) = 0 Then
            z = 1
            TeamIndex(i) = z
        Else
            If TypeOf usedRangeAs2DArray(i, 1) Is System.Double Then
                usedRangeAs2DArray(i, 1) = String.Concat(usedRangeAs2DArray(i, 1), ",00")
            End If
            If TypeOf usedRangeAs2DArray(i, 2) Is System.Double Then
                usedRangeAs2DArray(i, 2) = String.Concat(usedRangeAs2DArray(i, 2), ",00")
            End If
            z = usedRangeAs2DArray(i, 1) / usedRangeAs2DArray(i, 2)
            z = Math.Floor(100 * z) / 100
            TeamIndex(i) = z
        End If
        Next

UPDATE: Found better solution, convert both values to Double and replace "," with ".".

Here is code for this:

 Dim fDD, sDD As Double

                    If IsNumeric(usedRangeAs2DArray(i, 2)) And IsNumeric(usedRangeAs2DArray(i, 1)) Then
                        fDD = CDbl(usedRangeAs2DArray(i, 1).ToString().Replace(",", "."))
                       sDD = CDbl(usedRangeAs2DArray(i, 2).ToString().Replace(",", "."))
                    End If

                    z = fDD / sDD
                    z = Math.Floor(100 * z) / 100
                    TeamIndex(i) = z
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top