Ngọc Duy - 1 year ago 54

Vb.net Question

I have expression string:

`dim str as string = "999999999 * 999999999"`

I use DataTable.Compute to calculate:

`dim dt as new datatable`

dim result as double = 0

result = cdbl(dt.Compute(str))

And I get an error:

`Value is either too large or too small for Type 'Int32'.`

How can I control result datatype in this case?

This function work like Windows's Calculator. Code so confuse but it work :)

`'input = "999,999,999 x 888,888,888 + 112,365 ÷ 15 − 987,653"`

Public Shared Function strCalc(ByVal input As String) As String

If input.Substring(input.Length - 1, 1) = " " Then input = input.Substring(0, input.Length - 3)

input = input.Replace("−", "-").Replace("x", "*").Replace("÷", "/").Replace(",", "")

Dim temp As Double = 0

Dim arr() As String = input.Split(" ")

If arr.Length > 1 Then

temp = New DataTable().Compute(If(arr(0).Contains("."), arr(0), arr(0) & ".0") & " " & arr(1) & " " &

If(arr(2).Contains("."), arr(2), arr(2) & ".0"), "")

If arr.Length > 3 Then

For i As Integer = 3 To arr.Length - 1 Step 2

temp = New DataTable().Compute(temp & " " & arr(i) & " " &

If(arr(i + 1).Contains("."), arr(i + 1), arr(i + 1) & ".0"), "")

Next

End If

Return temp

End If

Return input

End Function

Answer

Runtime is treating your expression "999999999 * 999999999" as a multiplication of two integer values and trying to return it as an object which is the return type of Compute API. The output of multiplication of those numbers is resulting in a very large value which is crossing the maximum value which can be stored in a variable of int (System.Int32) data type. It results in arithmetic overflow exception. To give a hint to the runtime so that it considers the expression as a multiplication of two `double`

numbers please use following code:

`dim str as string = "999999999.0 * 999999999.0"`

Source (Stackoverflow)