c# - String contains trailing zeroes when converted from decimal -


i've run unusual quirk in program i'm writing, , trying figure out if knew cause. note fixing issue easy enough. can't figure out why happening in first place.

i have winforms program written in vb.net displaying subset of data. contains few labels show numeric values (the .text property of labels being assigned directly decimal values). these numbers being returned dll wrote in c#. dll calls webservice returns values in question. returns 1 string, other decimal (i don't have control on webservice, consume it). dll assigns these properties on object (both of decimals) returns object winform program called dll. obviously, there's lot of other data being consumed webservice, no other operations happening modify these properties.

so, short version is:

  • winform requests new foo dll.
  • dll creates object foo.
  • dll calls webservice, returns someotherfoo.

    //both foo.bar1 , foo.bar2 decimals foo.bar1 = decimal.parse(someotherfoo.bar1); //someotherfoo.bar1 string equal "2.9000" foo.bar2 = someotherfoo.bar2; //someotherfoo.bar2 decimal equal 2.9d 
  • dll returns foo winform.

    winform.lblmocklabelname1.text = foo.bar1 //inspecting foo.bar1 indicates value 2.9d winform.lblmocklabelname2.text = foo.bar2 //inspecting foo.bar2 indicates i'm 2.9d 

so, what's quirk?

winform.lblmocklabelname1.text displays "2.9000", whereas winform.lblmocklabelname2.text displays "2.9".

now, know c# , vb indicates format of string parsed decimal should have no bearing on outcome of later decimal.tostring() operation called on same decimal. expect decimal.parse(somedecimalstring).tostring() return string without trailing zeroes. find online seems corroborate (there countless stack overflow questions asking opposite...how keep formatting initial parsing).

at moment, i've removed trailing zeroes initial string gets parsed, has hidden quirk. however, i'd love know why happens in first place.

it's because scaling factor preserves trailing zeros in decimal number. trailing zeros not affect value of decimal number in arithmetic or comparison operations. however, trailing zeros might revealed tostring method if appropriate format string applied.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -