serializable double with precision

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steph

    serializable double with precision


    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 :

    <price100 </ price>

    And i want:

    <price100.00 </ price>

    How do ?

    thanks
  • Marc Gravell

    #2
    Re: serializable double with precision

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

    [XmlIgnore]
    public double Price {...}

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

    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


    Comment

    • Steph

      #3
      Re: serializable double with precision

      Marc Gravell wrote:
      Well, you could perhaps do it manually? I wouldn't do the entire
      serialization yourself, but how about:
      >
      [XmlIgnore]
      public double Price {...}
      >
      [XmlElement("Pri ce")]
      public string PriceString {
      get {return (your expicit Price.ToString( ) or whatever);}
      set {Price = double.Parse(va lue);}
      }
      >
      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(Elem entName = "paye")]
      [XmlIgnore]
      public double Paye
      {
      get { return _paye; }
      set { _paye = value; }
      }
      [XmlElement("pay e")]
      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 !

      Comment

      Working...