I'm working with an existing SQL Server database, with a table column defined as decimal(4,2)
. However, I've been asked to prevent the code from rounding decimal places.
So I tried changing the column type to decimal
, which is automatically converted to decimal(18, 0)
, which promptly wiped out any fractional portion of my existing values. (Nice. The default decimal type is an integer. What's the point?)
So I tried changing the column type to decimal(8,5)
. Since 5 digits after the decimal is more than needed, this eliminates the rounding issue.
However, whenever I print this value in a C# ASP.NET application, it is always formatted with 5 digits after the decimal (e.g. 12.34000
). I don't understand this. When I read the value from the database, I assign it to a regular decimal
value. So how does the decimal
value "know" there should be 5 digits after the decimal point?
More importantly, how can I display this value without the trailing zeros? I'm guessing there is a way to format the value. However, since it's used in many places it would be much better if it simply didn't automatically append trailing zeros. If needed, I can change the data type in the database.