String contains trailing zeros when converting from decimal

I had an unusual quirk in the program that I am writing, and I tried to find out if anyone knows the reason. Please note that the fixing problem is quite simple. I just can't understand why this is happening in the first place.

I have a WinForms program written in VB.NET that displays a subset of the data. It contains several labels that show numeric values ​​(the .Textlabels property is assigned directly from decimal values). These numbers are returned by a DLL written by me in C #. The DLL calls the web service, which initially returns the values ​​in question. It returns one as a string, the other as decimal (I have no control over the web service, I just consume it). The DLL assigns them to the properties of the object (both of which are decimal), and then returns this object back to the WinForm program that called the DLL. Obviously, there is a lot of other data from the web service, but no other operations occur, which can change these properties.

So, the short version:

  • WinForm requests a new one Foofrom the DLL.
  • The DLL creates the object Foo.
  • The DLL calls the webservice, which returns SomeOtherFoo.

    //Both Foo.Bar1 and Foo.Bar2 are decimals
    Foo.Bar1 = decimal.Parse(SomeOtherFoo.Bar1); //SomeOtherFoo.Bar1 is a string equal to "2.9000"
    Foo.Bar2 = SomeOtherFoo.Bar2; //SomeOtherFoo.Bar2 is a decimal equal to 2.9D
    
  • The DLL returns Foo to WinForm.

    WinForm.lblMockLabelName1.Text = Foo.Bar1 //Inspecting Foo.Bar1 indicates my value is 2.9D
    WinForm.lblMockLabelName2.Text = Foo.Bar2 //Inspecting Foo.Bar2 also indicates I'm 2.9D
    

So what is a fad?

WinForm.lblMockLabelName1.Text is displayed as "2.9000" , while WinForm.lblMockLabelname2.Text is displayed as "2.9" .

, # VB, , , , .ToString(), . , decimal.Parse(someDecimalString).ToString() - . , , , ( , ... ).

, , . , , .

+4
1

, . . ToString, .

+4

All Articles