-1

I'm trying to floor values after the decimal point. The requirement is if i have a value for say 0.547 the output should be 0.55 ; if it is 0.544 it should be 0.54. Could'nt find any inbuilt C# function to do this as well. I could write my own method to do this for limited decimal places in the future it can be any number of decimal places. So seeking if there's an inbuilt method which does this. Please help out.

bharathN
  • 11
  • 3
  • 2
    Did you find `Math.Round`? Note that you *don't* want to floor values, if you want to round 0.547 up to 0.55. (A floor of that would be 0.54, assuming you specify two decimal places.) – Jon Skeet Sep 27 '19 at 14:10
  • Check this link: [link](https://learn.microsoft.com/en-us/dotnet/api/system.math.round?view=netframework-4.8) – Ivo Oostwegel Sep 27 '19 at 14:12
  • Possible duplicate of https://stackoverflow.com/questions/2357855/round-double-in-two-decimal-places-in-c – Mark Cilia Vincenti Sep 27 '19 at 14:21

2 Answers2

1

Yes, there is a build-in function to do just this. The 2nd parameter specifies the # of digits. Note that in your example you are rounding not flooring.

Math.Round(0.547, 2)

This outputs 0.55

Carlo Bos
  • 3,105
  • 2
  • 16
  • 29
0
double x = 0.547;
x = Math.Round(x, 2); 

Result: x = 0.55

double x = 0.544;
x = Math.Round(x, 2); 

Result: x = 0.54

Hasan Kaan TURAN
  • 391
  • 2
  • 13