none
Decimal into string conversion shows different decimal values in different windows systems RRS feed

  • Question

  • I have a c# code running in .net framework 2.0 that when converting a decimal into a string to be printed in a file produces different outputs in different Windows OS.

    I have checked already that both system reginal settings are exactly the same, same language, same numer format etc.

    My code looks like this

    object o = dr.GetValue(dr.GetOrdinal(cCol.ColumnName));
    string value = Convert.ToString(o);

    It is getting a value from a database and the object is of type Decimal (o.GetType()==typeof(Decimal) evaluates to true)

    The problem is that when this program runs in .net Framework 2.0 but in a windows 2000 server the output is not the same as running in .net framework 2.0 in windows 10.

    For example for the value 2016

    Windows 2000 output: "2016"

    Windows 10 output: "2016.00"

    I have tried the following

    string value = ((Decimal)o).ToString("0.##"));

    Then the output is the same for the value 2016. However, and here is where it gets tricky, if the value is let's say 2016.6, the output with this new method is:

    Windows 2000 output: "2016.60"

    Windows 10 output : "2016.6"

    So there is no way I can't get it to output the same in both systems.

    What can be the source of the problem? Is there any way I can get in windows 10 the same output as Windows 2000?

    Thank you

    Thursday, October 3, 2019 8:34 AM

Answers

  • Thank you Karen,

    Actually you are half right, the code is not the problem, but the regional settings are the same in both machines. I found the problem to be in the way the db driver returns the data type. So it is actually an error in the db driver and not the code.

    Thanks

    • Marked as answer by En-coder Thursday, October 3, 2019 10:10 AM
    Thursday, October 3, 2019 10:10 AM

All replies


  • Regards Les, Livingston, Scotland

    Thursday, October 3, 2019 9:38 AM
  • Hello,

    A thought is code is not the problem but instead region and language settings differ between the two systems. These settings are in control panel -> region and language -> additional settings


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 3, 2019 9:55 AM
    Moderator
  • Thank you Karen,

    Actually you are half right, the code is not the problem, but the regional settings are the same in both machines. I found the problem to be in the way the db driver returns the data type. So it is actually an error in the db driver and not the code.

    Thanks

    • Marked as answer by En-coder Thursday, October 3, 2019 10:10 AM
    Thursday, October 3, 2019 10:10 AM