question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked Hobbyistprogrammer-7674 commented

Problem in calculation with different region (point and comma)

Hallo,

I have lot of xml tables with decimal numbers and they are formatted according to english or with dot (.) . I have an another system which has German system or it works with comma ( , ). My program converts all my xml table to List of objects when it loads, When i open my program in German system the converted list does not contain any decimals because in german system it takes . as comma.

how do i prevent my xmls being translated to list with local settings or what is the right method to approach or solve the problem?

Thanks

dotnet-visual-basic
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Does this, "...system which has German system..." mean that the default culture is German? If the data is updated on the German system will the XML still be stored in English format?

0 Votes 0 ·

Yes , the default culture is German. I am not storing it in xml. I have data already in XML which is with english culture. when my program loads xml will be converted to data table and then list of objects. when it is converted to list of objects all decimal information is gone. it thinks as thousand separator.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Hobbyistprogrammer-7674 commented

Try adding this line before the code that converts the data incorrectly:

 CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")

If it does not work, show more details about your code that converts the data.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hallo Viorel,

It works if include your line before like below
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntD.GetGenericArguments()(0))))

but it does not work if i do it like this

Dim cInfo As CultureInfo = CultureInfo.GetCultureInfo("en-US")
prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntD.GetGenericArguments()(0), cInfo)))

Any idea what i am doing wrong?

0 Votes 0 ·
Viorel-1 avatar image Viorel-1 Hobbyistprogrammer-7674 ·

What row(column) is (a string?) and what ntD.GetGenericArguments()(0) is?

The next example seems to work:

 Dim c = CultureInfo.GetCultureInfo("en-US")
 Dim r = Convert.ChangeType("123.45", GetType(Decimal), c)

0 Votes 0 ·
  Public ntI As Type = Type.GetType("System.Nullable`1[System.Int32]")
  Public ntD As Type = Type.GetType("System.Nullable`1[System.Double]")   

it is for checking if the property is nullable type. i posted below my full code.

0 Votes 0 ·
Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 answered

Thanks Viorel, It worked .. but below is my code i use to convert my datatable to List of Objects.

     Public ntI As Type = Type.GetType("System.Nullable`1[System.Int32]")
     Public ntD As Type = Type.GetType("System.Nullable`1[System.Double]")    
    
 Private Function ConvertDataTable(Of T As New)(table As DataTable) As List(Of T)
         Return New List(Of T)(table.AsEnumerable().Select(Function(row) GetItem(Of T)(row)))
     End Function
    
     Public Function GetItem(Of T As New)(row As DataRow) As T
         Dim item As New T
         Dim itemType = GetType(T)
         For Each column As DataColumn In row.Table.Columns
             Dim prop = itemType.GetProperty(column.ColumnName)
             If prop IsNot Nothing Then
                 If prop.PropertyType.Equals(ntI) Then
                     prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntI.GetGenericArguments()(0))))
                 ElseIf prop.PropertyType.Equals(ntD) Then
                     CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
                     prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntD.GetGenericArguments()(0))))
                 Else
                     prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), prop.PropertyType)))
                 End If
             End If
         Next
    
         Return item
    
    
     End Function
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YitzhakKhabinsky-0887 avatar image
0 Votes"
YitzhakKhabinsky-0887 answered Hobbyistprogrammer-7674 commented

HI @Hobbyistprogrammer-7674,

In the XML-Schema spec a double/decimal data type needs to be represented with a dot.

Please check here for a possible solution: xml-deserialization-of-double-value-with-german-decimal-separator-in-c-sharp


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks for the reply. but at present i dont deal with deserialization. I am just converting xml -> data table -> Lists of Objects.

0 Votes 0 ·