MarcIT MarcIT - 23 days ago 6
C# Question

c# limit decimal value to 2 places

I found a ton of answers but none work for me, can anyone help me with this problem.
I have a formula witch gives a very extensive result and only want a value like for instance 1,25

I tried several codes but none worked, what i have now is:

uw = (adp * ufa + adv * ug + perv * wmk) / ac;
String.Format("{0:.##}", uw);
resposta.Text = uw.ToString();


resposta
is a
label
in witch i display the answer.
uw
is
decimal
.

I dont want to round the value, just limit the numbers.

Thansk for the assist,

Answer

This should work if you are OK with rounding:

uw = (adp * ufa + adv * ug + perv * wmk) / ac;
resposta.Text = uw.ToString("N2");

This should work if you don't want to round values:

uw = (adp * ufa + adv * ug + perv * wmk) / ac;
resposta.Text = (Math.Truncate(uw * 100) / 100).ToString();