Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the precision for Decimal numbers in C#

Tags:

c#

decimal

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 the Decimal operations globally like so Decimal.set({ precision: 15}). This means that the operation will return at most 15 decimal digits.

  • TypeScript:the operation 5/3 returns 1.66666666666667
  • C# the operation5m/3m returns 1.6666666666666666666666666667

Is there some similar setting for Decimal values in C# ? How can I accomplish this in C# ?

like image 345
Devid Avatar asked Jan 11 '18 16:01

Devid


People also ask

What is precision value in C?

So, precision means the number of digits mentioned after the decimal point in the float number. For example, the number 2.449561 has precision six, and -1.058 has precision three.

How do you write decimal numbers in C?

For example, 5.48958123 should be printed as 5.4895 if given precision is 4. In C, there is a format specifier in C. To print 4 digits after dot, we can use 0.4f in printf(). Below is program to demonstrate the same.

How do you set precision to float?

To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).


3 Answers

This isn't exactly what you're asking, but you could initialize a NumberFormatInfo object within the global scope and use it to format decimals. Here is an example:

using System.Globalization;  NumberFormatInfo setPrecision = new NumberFormatInfo();     setPrecision.NumberDecimalDigits = 2;     decimal test = 1.22223;  Console.Write(test.ToString("N", setPrecision)); //Should write 1.23  setPrecision.NumberDecimalDigits = 3; test = 5m/3m;  Console.Write(test.ToString("N", setPrecision)); //Should write 1.667 

MSDN Link: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo(v=vs.110).aspx

NumberDecimalDigits usage example: https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numberdecimaldigits(v=vs.110).aspx

like image 140
Jacob Barnes Avatar answered Nov 03 '22 22:11

Jacob Barnes


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);
like image 35
lucky Avatar answered Nov 03 '22 23:11

lucky


The documentation of Decimal.js states the following:

precision

The maximum number of significant digits of the result of an operation.

All functions which return a Decimal will round the return value to precision significant digits except Decimal, absoluteValue, ceil, floor, negated, round, toDecimalPlaces, toNearest and truncated.

Well, if you really need that behavior globally, then simply implement a wrapper type that does that, you've got a good specification to go by:

public struct RoundedDecimal
{
    public static int Precision { get; private set; }

    public static Decimal AbsoluteValue(
        RoundedDecimal d) => Math.Abs(d.value);

    //same with ceiling, floor, etc.

    private readonly decimal value;

    public RoundedDecimal(decimal d)
    {
        value = Decimal.Round(d, Precision);
    }

    public static void SetPrecision(int precision)
    {
        Precision = precision; /*omitted argument validation*/ }

     public static implicit operator Decimal(
         RoundedDecimal d) => d.value;

    public static explicit operator RoundedDecimal(
        decimal d) => new RoundedDecimal(d);

    public static RoundedDecimal operator +(
        RoundedDecimal left, RoundedDecimal right) =>
           new RoundedDecimal(left.value + right.value);

    //etc.
}

Performance wise this will not be very impressive but if its the behavior you need then, by all means, implement it!

DISCLAIMER: written code on my cell phone, so its bound to have bugs... simpy trying to get the idea across.

like image 42
InBetween Avatar answered Nov 03 '22 21:11

InBetween