Decimal places c#

Asked

Viewed 3,181 times

5

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 pass 270.618 only.

Thanks in advance

1 answer

4


You can:

Keep the value, just change the display:

decimal x = 270.61864847349717630804948048M;

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

Upshot: 270.619

Round up:

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

Console.WriteLine(y.ToString());

Upshot: 270.619

Or Truncate the value:

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

Console.WriteLine(z.ToString());

Upshot: 270.618

I put in the Dotnetfiddle

  • 1

    Thank you very much, it worked, I used the Math.Round(x,3)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.