Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding to at least 2 to 4 decimal places

Tags:

c#

.net

How can I round a decimal to at least 2 decimal places and have it kept as a decimal?

I know I can do this, but it has a code smell.

var myResult = Decimal.Parse(myDecimal.ToString("0.00##"));

These are the expected results.

0.028 -> 0.028
0.02999 -> 0.03
like image 946
Daniel A. White Avatar asked Jul 29 '11 11:07

Daniel A. White


1 Answers

You can use

Math.Round(myResult, 4);
like image 152
Bas Avatar answered Oct 27 '22 12:10

Bas