C# 不四舍五入

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

当我将此对象作为 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("#.##"));

关于C# 不四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17109514/

相关文章:

c# - 为什么 TaskFactory.StartNew 收到 CancellationToken

javascript - JavaScript 中的 xml 到 json

javascript - 当我指定 JSON 时, Node GET 请求返回 html?

c# - 键对值 Linq

c# - 为什么 Linq 更快

javascript - AngularJS + SPA - 更改布局

c# - 如何删除为正常 I/O 打开的文件?

json - 在 shell 中处理 json 时,如何正确地将多个 jq 语句链接在一起,例如使用 curl?

ASP.NET - LINQ 的优势

c# - 如何绑定(bind)到键值对列表