tejas_grande
tejas_grande

Reputation: 283

C# Round Value of a Textbox

I have a small calculator that I am creating in C# (Sharp Develop). The user enters two values and the code returns the third. I am having trouble rounding the third value once it is returned. I have been through a couple of forums and the msdn site and I understand the code that is posted there, but I cant seem to make it work in my situation. Can anyone provide a little help? Reference the code below.

int y; 
decimal x, z;
x = int.Parse(tb2_fla.Text);      
y = int.Parse(tb2_e.Text);
z = (x * y * 1.732050808m) / 1000;  
tb2_kva.Text = z.ToString();

I welcome both assistance and criticism
Greg

Upvotes: 2

Views: 7150

Answers (5)

Mark Brackett
Mark Brackett

Reputation: 85685

Use Math.Round. Or, since you're going into a string, you could use either the Standard Numeric Format Strings, or the Custom ones.

Math.Round(z, 2).ToString();
z.ToString("0.00");

Upvotes: 4

JamesSugrue
JamesSugrue

Reputation: 15011

Look up Numeric Format Specifiers in the help. Something like:

tb2_kva.Text = String.Format("{0:d2}", z);

to format to 2dp

Upvotes: 0

Ian Jacobs
Ian Jacobs

Reputation: 5501

Try using Math.Round()

tb2_kva.Text = Math.Round(z, # Places).ToString();

Upvotes: 0

Kevin Tighe
Kevin Tighe

Reputation: 21201

Try the Math.Round function.

Upvotes: 0

terjetyl
terjetyl

Reputation: 9565

Could Math.Round(z, nrofdecimals) be the answer to your problem?

Upvotes: 0

Related Questions