All,
I have a set decimal values in C#. I am writing these to fields in a class then serialising this class to an XML data file. However, I have an issue when trying to round a value that is stored as zero or 0, to four decimal places. I want 0 to be stored as 0.0000. Is this possible using decimal? Of course I can do this without issue when casting to string, and this is not the problem. I have read many threads but none seem to address this issue.
Any help would be most appreciated. The code snippit I have is:
// directCost (dCost).
decimal dCost = Convert.ToDecimal(EpiCostValues[(int)epiCostField.directCost]);
dCost = decimal.Round(dCost, 4, MidpointRounding.AwayFromZero);
episodeCostTmp.dCost = dCost;
// indirectCost (iCost).
decimal iCost = Convert.ToDecimal(EpiCostValues[(int)epiCostField.indirectCost]);
iCost = decimal.Round(iCost, 4, MidpointRounding.AwayFromZero);
episodeCostTmp.iCost = iCost;
But this does not force 0 to 0.0000, which is a problem for the required format of the .xml data file.
Add this line into your code
For Eg:
Hope this Helps 🙂