Rounding half down a decimal

后端 未结 3 1190
借酒劲吻你
借酒劲吻你 2021-01-25 07:52

Does an equivalent of Java RoundingMode.HALF_DOWN exist in C#?

For example, I want to round 1.265 to 1.26, and 1.266

3条回答
  •  渐次进展
    2021-01-25 08:41

    Have a look at Math.Round e.g.

      double[] tests = new double[] {
        1.265,
        1.266,
      };
    
      var demo = tests
        .Select(x => $"{x} -> {Math.Round(x, 2, MidpointRounding.AwayFromZero)}");
    
      var report = string.Join(Environment.NewLine, demo);
    
      Console.Write(report);
    

    Outcome:

      1.265 -> 1.26
      1.266 -> 1.27
    

提交回复
热议问题