C# serialize decimal to xml

前端 未结 2 884
情深已故
情深已故 2021-02-18 14:01

I got a decimal property, like

[XmlElementAttribute(DataType = \"decimal\")] decimal Price

The problem is that I wanna force it to serialize always

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-18 14:50

    I was having the opposite problem. My decimals were serializing with 4 decimal places, even though they were all 4 zeroes. I discovered that if I call decimal.Round(value, 2) then it serializes to 2 decimal places. It would appear that the Decimal type remembers what you last rounded it too when it is serialized.

    I was suspicious of the suggestion, but it worked that simply. Even though the value didn't need rounding, calling Round changed how many decimal places showed up in serialization.

提交回复
热议问题