Skip to main content
 首页 » 编程设计

C# 不四舍五入

2024年02月13日12emanlee

当我将此对象作为 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)?

请您参考如下方法:

使用;

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

参见 this answer了解更多信息

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

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