【问题标题】:C# Not Rounding DoubleC# 不舍入双精度
【发布时间】:2013-06-14 13:22:05
【问题描述】:

当我将此对象作为 JSON 传回时,它看起来像这样:

0.000000000000000e+000

我的 C# 代码是:

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        adjustment = Math.Round((double)s.adjustment, 2, MidpointRounding.AwayFromZero).ToString(),
        isT_E = (bool)s.isTimeAndExpense
    };

我怎样才能使它只四舍五入到小数点后两位 (0.00)?

【问题讨论】:

  • 为什么?小数位仅在呈现给用户时才重要。
  • 它四舍五入到小数点后 2 位,但仍以double 的形式存储,根据定义,它还有更多小数位。正如 Henk 所说,您需要使用 JavaScript 呈现任何您喜欢的内容。
  • @ElRonnoco 我将其存储为字符串(adjustment 是字符串,而 s.adjustment 是双精度)
  • @HenkHolterman 我将它作为字符串传递,而不是双精度。所以,这里的演示很重要。
  • @GrantWinney s.adjustment?double(可选双精度)。但是,adjustmentstring

标签: c# json linq linq-to-sql asp.net-mvc-4


【解决方案1】:

使用;

dec.ToString("#.##");

更多信息请参见this answer

如果它是 Console 应用程序中的可为空的 double;

    double ? d = 2.22222;
    Console.WriteLine(d.Value.ToString("#.##"));

【讨论】:

  • 语法错误:No overload method for 'ToString' takes 1 arguments。代码:adjustment = s.adjustment.ToString("#.##"),
  • @user1477388 那你确定 s.adjustment 是双倍的吗?
  • @Tory 它是?double(因此是演员表)。
  • @GrantWinney 现在,我收到一个 LINQ to SQL 错误 Method 'System.String ToString(System.String)' has no supported translation to SQL. 但至少更接近了。
  • 如果它在数据库中存储为可以为空的双精度,那么我会想到将一个字符串推入其中。
【解决方案2】:

我认为你混淆了两件事。 “真实”数字不是您所看到的。实数以二进制格式在内部存储。您看到的十进制数字在这种内部格式中不存在。您看到的是将此值转换为字符串形式的十进制表示。

将任何内部二进制表示转换为人类可见的字符串称为格式化Round 函数执行 not 格式。看这个例子:

double x = 0.123456000000000e+000;
double y = Math.Round(x, 2, MidpointRounding.AwayFromZero);
// y ====> 0.120000000000000e+000;

舍入函数改变内部值。您需要的可能不是更改值,而是仅显示两位未更改的值:

string formattedValue = x.ToString("N2");

如果您使用货币进行交易,请使用decimal 而不是doubledecimal 在内部使用二进制编码的十进制格式。像 1/10 这样的值在计算机中不能精确地表示为二进制数,就像 1/7 不能用十进制表示法(0.142857142857...)精确表示一样。但是 1/10 在存储为 decimal 时具有精确的内部表示。

【讨论】:

  • 谢谢,这很有道理。但是,这最终不会按原样(内联)工作,因为 LINQ to SQL 以它的方式工作。唯一对我有用的解决方案是我在这里找到并发布的那个stackoverflow.com/a/17109950/1477388
【解决方案3】:

原来,这是一个 LINQ to SQL 问题。我这样做了,它有效...

// get adjustments for user
IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        adjustment = roundtest(s.adjustment.GetValueOrDefault()),
        isT_E = (bool)s.isTimeAndExpense
    };

// test method...
public string roundtest(double num)
{
    return num.ToString("#.##");
}

【讨论】:

    【解决方案4】:

    试试这个:

    // get adjustments for user
    IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        adjustment = s.adjustment.GetValueOrDefault().ToString("0.##"),
        isT_E = (bool)s.isTimeAndExpense
     };
    

    -编辑-

    我认为你可能需要让 Severity 类有一个属性,该属性采用双精度并将字符串保存到 Severity.adjustment,如下所示:

     Severity
     {
          //rest of class as normal
    
          public double SetAdjustment
               {
                    set { adjustment = value.ToString("0.00"); } }
               }
     }
    

    -编辑,第 2 部分-

    // get adjustments for user
    IEnumerable<Severity> existingSeverities = 
    from s in db.AdjusterPricingGrossLossSeverities
    where s.type == type
    && s.adjusterID == adj.id
    select new Severity
    {
        id = s.severity,
        SetAdjustment = s.adjustment.GetValueOrDefault(),
        isT_E = (bool)s.isTimeAndExpense
     };
    

    您的代码的其余部分不需要更改,它仍应正常使用 (Severity variable).adjustment。这只是为了避免无法保证将 .Net 的 Standard Numeric Format Strings 转换为 SQL 的 Convert 的方法,更不用说任何自定义格式。

    【讨论】:

    • 谢谢,但我收到错误 Method 'System.String ToString(System.String)' has no supported translation to SQL.
    • 如何在我的 LINQ to SQL 查询的上下文中使用您的方法?
    猜你喜欢
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多