Serialization of Double gives a '1E-12' - I want 0.000000000001

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey

I am running into this issue.

I have a Serializable Class that has a Double member.

I am using XmlSerializer to serilaize this object,
If the Double member is large (or very small) , the XmlSerializer is
converting this to its Scientific Notation (ie. 0.000000000001 -> '1E-12')

I dont want '1E-12' but instead I want 0.000000000001

Any suggestions?

Thanks
 
Nutshell said:
I have a Serializable Class that has a Double member.

I am using XmlSerializer to serilaize this object,
If the Double member is large (or very small) , the XmlSerializer is
converting this to its Scientific Notation (ie. 0.000000000001 -> '1E-12')

I dont want '1E-12' but instead I want 0.000000000001

I'm not clear on why the representation of the number is important. The
two strings are equivalent. But if it is, you should be able to
implement ISerializable for your class to override the default behavior.
Then you can use whatever custom formatting you like to ensure that
the serialized data has the exact representation you want.

Pete
 
Thanks Peter -

The issue is that I do need the number representation.

And unfortunately , I have 100+ classes that I would need to implement an
ISerializable on -

you wouldnt happen to have a short example :)
 
Nutshell said:
The issue is that I do need the number representation.

And unfortunately , I have 100+ classes that I would need to implement an
ISerializable on -

you wouldnt happen to have a short example :)

MSDN does a fine job of documenting ISerializable, so I don't think I
need to provide an example of that. An example of dealing with a
hundred classes with one simple change, I don't think exists. I'm sure
not aware of a way to do that.

Of course, you could implement your own version of Double that
serializes the way you want, and then use that instead of the built-in
Double everywhere. You'd have to touch every class, but if you do it
right it should amount to little more than a global search-and-replace.

Another option: you could insert your own Stream in between the
serializing and the file output, looking for Doubles serialized as
scientific notation and expanding each one as you desire. You serialize
to your own stream rather than the destination stream, and your stream
would write the modified results to the destination stream.

Me, I'd rather try to change the requirements than hack serialization
that way. But that's not always possible, and I think you could
probably get what you wanted by doing that.

I'm assuming here that the serialized data is a text stream of some
sort, but since you're concerned about the string representation of the
serialized data, that seems like a safe assumption.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top