XMLSerializer, customize float serialization

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

    XMLSerializer, customize float serialization

    Hello,
    I am using the XmlSerializer clas to serialize/deserialize XML files.
    I have a XML file containing elements which have attributes of type
    float. If the value of the float attribute in my application is for
    example 43.5678, after the serialization I get the value
    "43.56779999999 99". Is it any way to control the formatting of the float
    type, to limit for example the number of positions after the comma ?
    The only solution that I found is to change the attribute type from
    float to string, and format the string programmaticall y by myself ... Is
    there any better solution available ? I did not found any method to
    overload the "ToString() " method for the floats ... And also the
    XmlWriterSettin gs class does not offer any interface to control the
    float format ...
    Thanks in advance for your help.
    Regards,
    Abra


    *** Sent via Developersdex http://www.developersdex.com ***
  • ssamuel

    #2
    Re: XMLSerializer, customize float serialization

    Abra,

    The string method isn't a bad solution. Just make sure you either
    TryParse (preferrable) or catch NumberFormatExc eption (less preferred)
    on deserialization .

    Part of the problem is that you don't really want a float. The datum
    you're trying to convey in your example is better cast into a decimal
    type than a float type. If you were using decimal, you wouldn't have
    this problem.


    Stephan



    Abra wrote:
    Hello,
    I am using the XmlSerializer clas to serialize/deserialize XML files.
    I have a XML file containing elements which have attributes of type
    float. If the value of the float attribute in my application is for
    example 43.5678, after the serialization I get the value
    "43.56779999999 99". Is it any way to control the formatting of the float
    type, to limit for example the number of positions after the comma ?
    The only solution that I found is to change the attribute type from
    float to string, and format the string programmaticall y by myself ... Is
    there any better solution available ? I did not found any method to
    overload the "ToString() " method for the floats ... And also the
    XmlWriterSettin gs class does not offer any interface to control the
    float format ...
    Thanks in advance for your help.
    Regards,
    Abra
    >
    >
    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Shailen Sukul

      #3
      RE: XMLSerializer, customize float serialization

      Read my post at
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      for information on how to implement custom serialization.
      --
      Good luck!

      Shailen Sukul
      Architect
      (BSc MCTS, MCSD.Net MCSD MCAD)
      Ashlen Consulting Service P/L
      (http://www.ashlen.net.au)


      "Abra" wrote:
      Hello,
      I am using the XmlSerializer clas to serialize/deserialize XML files.
      I have a XML file containing elements which have attributes of type
      float. If the value of the float attribute in my application is for
      example 43.5678, after the serialization I get the value
      "43.56779999999 99". Is it any way to control the formatting of the float
      type, to limit for example the number of positions after the comma ?
      The only solution that I found is to change the attribute type from
      float to string, and format the string programmaticall y by myself ... Is
      there any better solution available ? I did not found any method to
      overload the "ToString() " method for the floats ... And also the
      XmlWriterSettin gs class does not offer any interface to control the
      float format ...
      Thanks in advance for your help.
      Regards,
      Abra
      >
      >
      *** Sent via Developersdex http://www.developersdex.com ***
      >

      Comment

      Working...