简体   繁体   English

如何保持两位数精度

[英]How to preserve the 2 digit precision

I'm struggling to find a better way to preserve the 2 digit precision for decimal variable. 我正在努力寻找一种更好的方法来保留十进制变量的2位精度。

Here I have divided the question in 2 steps: 在这里,我将问题分为两个步骤:

Step1: SQL Query 步骤1: SQL查询

Inorder to get a 2 digit precision in a SQL query, I'm using 为了在SQL查询中获得2位精度,我正在使用

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

On execution, I'm able to achieve the 2 digit precision in my SSMS. 执行后,我能够在我的SSMS中达到2位数的精度。

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

Step2: via C# code 步骤2: 通过C#代码

Whereas in my C# code, when I tried to store the value in my C# code it is not preserving the 2 digit precision. 而在C#代码中,当我尝试将值存储在C#代码中时,它并没有保留2位数的精度。

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. FileHelper.dll用于将查询列表转换为CSV文件。

I would like to know the reason and a way to solve this problem. 我想知道原因和解决此问题的方法。

Any help is much appreciated. 任何帮助深表感谢。

Thanks in advance. 提前致谢。

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));

You can use 您可以使用

decimal.Round(value,decimalPoints);

This will return a decimal rounded upto decimalPoints. 这将返回一个小数点后四舍五入到十进制点数。

You are confusing precision with representation. 您将精度与表示混淆了。 What you see in SSMS is a formatted number (a string). 您在SSMS中看到的是带格式的数字(字符串)。 What you have in C# (and inside the DB) is a "true" number. 您在C#中(和数据库中)拥有的是一个“真实”数字。 In a true normally ending 0 after the decimal point aren't included. 在true中,通常不包括小数点后的0。

If you want to format it you can do something like NET_TOTAL.ToString("0.00") 如果要格式化它,可以执行NET_TOTAL.ToString("0.00")

Note that there could be another problem: 请注意,可能还有另一个问题:

NET_TOTAL = 1.245;

decimal sum = NET_TOTAL + NET_TOTAL;

the sum will be 2.29, but after a roundtrip to the DB NET_TOTAL will be 1.24 or 1.25 (depending on how the DB rounded 1.245) and the recalculated sum will be 2.28 or 2.3 . sum为2.29,但到DB NET_TOTAL的往返后将为​​1.24或1.25(取决于DB如何舍入为1.245),并且重新计算的总和将为2.28或2.3。

is this acceptable: 这可以接受吗?

  private decimal? _netTotal; public decimal? NET_TOTAL{ get{ return _netTotal; } set { if (value.HasValue) { _netTotal = Math.Truncate(value.Value, 2); // or rounding }else{ _netTotal = value; } } } 

if you want to keep the original value but present it with 2 decimal places, then you could modify the getter instead: return Math.Truncate(value.Value, 2); 如果要保留原始值但将其保留2个小数位,则可以修改getter:return Math.Truncate(value.Value,2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM