How to preserve the 2 digit precision

.net c# dapper decimal precision

Question

I'm struggling to find a better way to preserve the 2 digit precision for decimal variable.

Here I have divided the question in 2 steps:

Step1: SQL Query

Inorder to get a 2 digit precision in a SQL query, I'm using

CONVERT(DECIMAL(10,2), .....) // to get 2 digit precision

On execution, I'm able to achieve the 2 digit precision in my SSMS.

NET_TOTAL  
---------    
10.00  
12.50  
14.25

Step2: via C# code

Whereas in my C# code, when I tried to store the value in my C# code it is not preserving the 2 digit precision.

NET_TOTAL  
---------    
10       //lost my precision
12.5     //lost my precision
14.25

Following is my variable declaration.

public decimal? NET_TOTAL { get; set; } 

But I can smell a workaround using properties, but I'm not able to get it.

I'm using

  • for database operations.
  • FileHelper.dll for converting the query list to a CSV file.

I would like to know the reason and a way to solve this problem.

Any help is much appreciated.

Thanks in advance.

Popular Answer

Trailing zeros to the right of the decimal is not precision.
It is just presentation.

decimal d;
d = 10;
System.Diagnostics.Debug.WriteLine(string.Format("{0:N2}",d));



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why