HighTechTalks DotNet Forums  

Decimal Serialization Inconsistency?

Dotnet XML microsoft.public.dotnet.xml


Discuss Decimal Serialization Inconsistency? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
James Koch
 
Posts: n/a

Default Decimal Serialization Inconsistency? - 10-30-2007 , 03:38 PM






I must be missing something here...

The inconsistency described below is minorly annoying in a current
project. Can anyone come up w/ an explanation?

I've got two seemingly identical object graphs which serialize to
XML. Each has some decimal properties, ex:

class Widget
{
public decimal SubTotal { get; set; }
public decimal Tax { get; set; }
}

When I serialize each graph, some of the decimals which are zero come
out as "0" while others come out as "0.00". Specifically:

widgetA <SubTotal>0</SubTotal><Tax>0</Tax>
widgetB <SubTotal>0.00</SubTotal><Tax>0</Tax>

Comparing the objects in memory, the following all evaluate to true:
widgetA.SubTotal == widgetB.SubTotal
widgetA.Tax == widgetB.Tax
widgetA.SubTotal * 1000000 == widgetB.SubTotal * 1000000
widgetA.SubTotal * 1000000m == widgetB.SubTotal * 1000000m

I'm trying to find some rhyme or reason to this behavior. Any ideas?
Thanks in advance!

James


Reply With Quote
  #2  
Old   
jasonterando@yahoo.com
 
Posts: n/a

Default Re: Decimal Serialization Inconsistency? - 12-14-2007 , 04:14 PM






The only thing I can figure out is that the "decimal" data type stores
the precision used to assign to it as well as the value itself. Your
post, though, inspired a solution to the problem. I'm using a class
generated by Microsoft's xsd.exe utility. The class contains a
decimal field and our client wants us to write out values with two
decimal places.

So, I tried the following, and it appears to work:

/// Override for Amount output
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Amount {
get {
return this.amountField;
}
set {
this.amountField = decimal.Parse(string.Format("{0:###0.00}",
value));
}
}





On Oct 30, 12:38 pm, James Koch <jamesk... (AT) gmail (DOT) com> wrote:
Quote:
I must be missing something here...

The inconsistency described below is minorly annoying in a current
project. Can anyone come up w/ an explanation?

I've got two seemingly identical object graphs which serialize to
XML. Each has some decimal properties, ex:

class Widget
{
public decimal SubTotal { get; set; }
public decimal Tax { get; set; }

}

When I serialize each graph, some of the decimals which are zero come
out as "0" while others come out as "0.00". Specifically:

widgetA <SubTotal>0</SubTotal><Tax>0</Tax
widgetB <SubTotal>0.00</SubTotal><Tax>0</Tax

Comparing the objects in memory, the following all evaluate to true:
widgetA.SubTotal == widgetB.SubTotal
widgetA.Tax == widgetB.Tax
widgetA.SubTotal * 1000000 == widgetB.SubTotal * 1000000
widgetA.SubTotal * 1000000m == widgetB.SubTotal * 1000000m

I'm trying to find some rhyme or reason to this behavior. Any ideas?
Thanks in advance!

James


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.