Answered by:
i like to round a double to 2 decimal.
Question

Answers

You can use Math.Round(double, digits) http://msdn.microsoft.com/enus/library/aa340228.aspx but 2,33... will be (correctly) approximated to 2.33, not to 2.34.
Matteo Migliore
Bloghttp://blogs.ugidotnet.org/matteomigliore
Twitterhttp://twitter.com/matteomigliore
CodePlex Marked as answer by Mike FengModerator Tuesday, May 15, 2012 1:50 PM

Matteo is right, but if you really want to obtain 2.34 (i.e., to round in any case to the greatest decimal), you can use this simple method:
public double Round(double number, double arr) { double n = number / arr; n = Math.Ceiling(n); return (double)n* arr; } var number = 2.33333333333; var rounded = Round(number, 0.01); // Returns 2.34
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva Edited by Marco MinervaMVP Wednesday, May 2, 2012 1:29 PM
 Proposed as answer by RohitArora Thursday, May 3, 2012 6:58 AM
 Marked as answer by Mike FengModerator Tuesday, May 15, 2012 1:49 PM
All replies

You can use Math.Round(double, digits) http://msdn.microsoft.com/enus/library/aa340228.aspx but 2,33... will be (correctly) approximated to 2.33, not to 2.34.
Matteo Migliore
Bloghttp://blogs.ugidotnet.org/matteomigliore
Twitterhttp://twitter.com/matteomigliore
CodePlex Marked as answer by Mike FengModerator Tuesday, May 15, 2012 1:50 PM

Matteo is right, but if you really want to obtain 2.34 (i.e., to round in any case to the greatest decimal), you can use this simple method:
public double Round(double number, double arr) { double n = number / arr; n = Math.Ceiling(n); return (double)n* arr; } var number = 2.33333333333; var rounded = Round(number, 0.01); // Returns 2.34
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva Edited by Marco MinervaMVP Wednesday, May 2, 2012 1:29 PM
 Proposed as answer by RohitArora Thursday, May 3, 2012 6:58 AM
 Marked as answer by Mike FengModerator Tuesday, May 15, 2012 1:49 PM