Decimal places c #

3

I have a variable of decimal type returning me the value 270.61864847349717630804948048 how do I return with only 3 houses after the comma, in case I pass 270.618 only.

Thank you in advance

    
asked by anonymous 12.04.2018 / 20:41

1 answer

5

You can:

Keep the value, just change the view:

decimal x = 270.61864847349717630804948048M;

Console.WriteLine(x.ToString("N3"));
  

Result: 270,619

Round the value:

decimal y = Math.Round(x,3);

Console.WriteLine(y.ToString());
  

Result: 270,619

Or Truncate value:

decimal z = Math.Truncate(x * 1000)  / 1000;     //1000 para 3 casas decimais   

Console.WriteLine(z.ToString());
  

Result: 270,618

I put it in DotNetFiddle

    
12.04.2018 / 20:50