473,382 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Decimal Serialization Inconsistency?

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

Oct 30 '07 #1
1 6049
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...@gmail.comwrote:
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
Dec 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rim | last post by:
Hi, With the great unification of types and classes, what will happen to the following identity inconsistency? >>> class myint(int): pass .... >>> a=int(1); b=int(1) >>> a is b 1
15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
2
by: Peter Clifton | last post by:
Hello all, I am very new to this subject and learning by example. I have a small inconsistency in an XML file when transformed using different processors. I was wondering if someone could shed...
3
by: Xavier Wargny | last post by:
Hi all and each! Since july, I'm working with dot NET technology. Confident with brand new tools it gave, I decided to use that new decimal type (related with the Database type...). After many...
2
by: Mike in Paradise | last post by:
Can you use the DefaultValueAttribute on Decimal PropertyFields? If so what is format for setting the Default value. Thanks... Example /// <summary> /// Field: increment value ///...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.