serializable double with precision

  • Thread starter Thread starter Steph
  • Start date Start date
S

Steph

When I serialise an object I would like to have a double precision in
the "double" type :

My code:
private double _price = 100;
public double Price
(
get (return;)
set (_price = value;)
}



Serialize :

<price> 100 </ price>

And i want:

<price> 100.00 </ price>

How do ?

thanks
 
Well, you could perhaps do it manually? I wouldn't do the entire
serialization yourself, but how about:

[XmlIgnore]
public double Price {...}

[XmlElement("Price")]
public string PriceString {
get {return (your expicit Price.ToString() or whatever);}
set {Price = double.Parse(value);}
}

If you do this, you may wish to explicitely use an invariant culture
in the ToString() / Format().

Also - if you are using data-contracts, note that you can happily make
PriceString private; if the intellisense annoys you can can use
[EditorBrowsable({blah}.Never)] to remove it from view.

Marc
 
When I serialise an object I would like to have a double precision in
the "double" type :

How are you serializing the data?

If you are serializing to text, the format is not going to be "double
precision" or "single precision" or "integer". It's going to be text. If
your value is an integer, even if it's stored in a double precision
variable, it's perfectly reasonable for the text to represent just the
integer.

If you are using the built-in serialization, then rest assured your data
will come back the same as it was before it was serialized. If you are
doing it yourself, you'll want to make sure you're using a text format
that is assured of making the round-trip safely. For example, using the
"round-trip" string format for your floating point value.

If the above does not answer your question, you will probably need to be
more specific and detailed about how you're serializing your data, what
you expect to happen, and what is actually happening.

Pete
 
Marc said:
Well, you could perhaps do it manually? I wouldn't do the entire
serialization yourself, but how about:

[XmlIgnore]
public double Price {...}

[XmlElement("Price")]
public string PriceString {
get {return (your expicit Price.ToString() or whatever);}
set {Price = double.Parse(value);}
}

If you do this, you may wish to explicitely use an invariant culture
in the ToString() / Format().

Also - if you are using data-contracts, note that you can happily make
PriceString private; if the intellisense annoys you can can use
[EditorBrowsable({blah}.Never)] to remove it from view.

Marc

it's perfect...
i use :
private double _paye = 0;
//[XmlElement(ElementName = "paye")]
[XmlIgnore]
public double Paye
{
get { return _paye; }
set { _paye = value; }
}
[XmlElement("paye")]
public string PayeString
{
get { return Paye.ToString("#0.00"); }
set { double.TryParse(value, out _paye); }
}
and i can continue to use my code ! (public object this[string index])
a minimum code change ! perfect !

great thanks for this tips !
 
No problem - but personally I'd be a little cautious of TryParse
here... if it fails, I *want* to know about it (rather than just
setting zero).

Also note the advisory comment about the invariant culture; this will
be a big problem if your data isn't very local (i.e. a French client
and a UK/US server would fail, as it would be contrasting commas and
periods).

Of course, "#0.00" isn't exactly what I would call double-precision
either... that is fixed 2-decimal digit precision. And if you are
wanting that, then decimal would make a much better field-type than
double.

At such, I would be using a decimal and:
get { return Paye.ToString("#0.00",
CultureInfo.InvariantCulture); }
set { _paye = decimal.Parse(value,
CultureInfo.InvariantCulture); }

But even then, your object isn't round-trip safe; if somebody gives it
a value of 1.234, and serializes and deserializes it, then they will
see 1.23; if that is the desired behavior, then fine - but personally
I'd be inclined to do this in the regular setter, so that the value is
always truncated in the set (or an exception is thrown if the value
contains more than 2 non-zero digits). Perhaps something like:

set {_paye = Math.Truncate(100 * value) / 100;}

Of course, in this case you probably don't even need the PayeString
property (or pehaps even store * 100 as an integer).

Marc
 

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