Round double in two decimal places in C#?

后端 未结 8 2107
忘掉有多难
忘掉有多难 2020-11-30 18:21

I want to round up double value in two decimal places in c# how can i do that?

double inputValue = 48.485;

after round up

i         


        
相关标签:
8条回答
  • 2020-11-30 18:39

    Use Math.Round

    value = Math.Round(48.485, 2);
    
    0 讨论(0)
  • 2020-11-30 18:42

    Use an interpolated string, this generates a rounded up string:

    var strlen = 6;
    $"{48.485:F2}"
    

    Output

    "48.49"
    
    0 讨论(0)
  • 2020-11-30 18:48
    Math.Round(inputValue, 2, MidpointRounding.AwayFromZero)
    
    0 讨论(0)
  • 2020-11-30 18:49

    I think all these answers are missing the question. The problem was to "Round UP", not just "Round". It is my understanding that Round Up means that ANY fractional value about a whole digit rounds up to the next WHOLE digit. ie: 48.0000000 = 48 but 25.00001 = 26. Is this not the definition of rounding up? (or have my past 60 years in accounting been misplaced?

    0 讨论(0)
  • 2020-11-30 18:58

    Another easy way is to use ToString with a parameter. Example:

    float d = 54.9700F;    
    string s = d.ToString("N2");
    Console.WriteLine(s);
    

    Result:

    54.97
    
    0 讨论(0)
  • 2020-11-30 19:02

    This works:

    inputValue = Math.Round(inputValue, 2);
    
    0 讨论(0)
提交回复
热议问题