0

I am diving 2 decimal number and want value upto 5 precision. below is my code.

decimal postiveTotal = 3,totalLenght = 6;
decimal postiveFractionResult = postiveTotal / totalLenght;

I m expecting 0.50000 but I am getting 0.5

maxspan
  • 13,326
  • 15
  • 75
  • 104
  • `String.Format("{0:0.00000}", postiveFractionResult ); ` https://dotnetfiddle.net/v7oVMe – Prasad Telkikar Apr 25 '18 at 05:09
  • 1st Question are you trying to print the value in string? – Lucifer Apr 25 '18 at 05:12
  • How do you want to use that? – SᴇM Apr 25 '18 at 05:20
  • 1
    As per my understanding, OP didn't ask for string conversion. What if OP wants to get expected results without converting it to string? – mmushtaq Apr 25 '18 at 05:21
  • 2
    Could you explain *why* you expect the result to be 0.50000? You could get that result by initializing `postiveTotal` to `3.00000m`, but I don't see why you'd expect to get 5 decimal places automatically. – Jon Skeet Apr 25 '18 at 05:24

1 Answers1

3

C# number display type like decimal will always strip the appended 0 - there is no difference between 0.5 and 0.50.

If you want to have the output correctly formatted you need to use a string format identifier:

Custom number format identifier:

Console.WriteLine($"{postiveFractionResult:0.00000}");

Standard number format identifier:

Console.WriteLine($"{postiveFractionResult:F5}");

Assignment to variables:

   // using string interpolation
   string result = $"{postiveFractionResult:0.00000}";
   // using string.format explicite
   string result = string.Format("{0:0.00000}", postiveFractionResult);

You can find more information on string format here.

EDIT

As noted by Daisy Shipton there is a difference when declaring a variable either by 0.5M or 0.50M. A little test with the different declaration shows that the additional defined 0 is also preserved through calculation:

var result  = 1.25m * 0.5m;         // 6.25M
var result1 = 1.250m * 0.5m;        // 0.6250M
var result2 = 1.250m * 0.50000m;    // 0.62500000M
var result3 = 1.25000m * 0.5m;      // 0.625000M
var result4 = 1.25000m * 0.50000m;  // 0.6250000000M

Please see also the following so post which has an explanation about this behavior. Sadly the links are broken and I could not find the correct ECMA link due to the website being currently offline.

dsdel
  • 1,042
  • 1
  • 8
  • 11
  • 1
    "there is no difference between 0.5 and 0.50" - yes there is, for decimal. Try `decimal x = 0.5m; decimal y = 0.50m; Console.WriteLine(x); Console.WriteLine(y);`. You'll see that the extra digit in `y` has been preserved. – Jon Skeet Apr 25 '18 at 05:42
  • 1
    @DaisyShipton thank you for your comment! I ment, that there is no real difference when calculating with the field (at least from math logic as in result). I have updated my answer to reflect your comment. – dsdel Apr 25 '18 at 05:55
  • 1
    For printing `Console.WriteLine( postiveFractionResult.ToString("F5"));` will work the same - it is the [standard numeric fixed point format](https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#FFormatString) - instead of using custom numeric formats. – Patrick Artner Apr 25 '18 at 07:14
  • @PatrickArtner Thank you ! Updated my post – dsdel Apr 25 '18 at 08:30