Set the precision for Decimal numbers in C#

前端 未结 3 963
逝去的感伤
逝去的感伤 2020-12-17 15:57

Is it possible to change the precision for Decimal numbers in C# globally ?

In TypeScript I am using the framework Decimal.js, where I can change the precision of th

3条回答
  •  忘掉有多难
    2020-12-17 16:32

    There is no generic setting for decimal precision. Your best chance is implementing these methods in your extensions.

    var decimalValue = 5m/3m;
    var str = decimalValue.ToString("0.##############");//1.66666666666667
    

    or you could use Round;

    var decimalValue = 5m/3m;
    decimalValue = decimal.Round(decimalValue, 6, MidpointRounding.AwayFromZero);
    

提交回复
热议问题