Niko Gamulin
Niko Gamulin

Reputation: 66565

How to round a double value to a selected number of decimals in .NET?

I use an acceleration sensor to calculate the current accelerations and it returns the double value.

However I would like to compare the current acceleration with value 9.8. Before doing that i have to round the value received from the sensor so the question is: How to round a double value to a selected number of decimals in .NET?

Upvotes: 1

Views: 839

Answers (2)

Nathan Koop
Nathan Koop

Reputation: 25197

Math.Round(number, precision)

http://msdn.microsoft.com/en-us/library/zy06z30k.aspx

Upvotes: 7

Marc Gravell
Marc Gravell

Reputation: 1062905

Math.Round - i.e.

double val = Math.Round(current, 1); // 1dp

Upvotes: 4

Related Questions