Round Float Number to two Decimal Places in C#

Last Reply one year ago By Mudassar

Posted one year ago

Hello,

I am doing substraction opearation on two textbox values but not getting exact result.

My code is

float x = float.Parse(txtx.Text);
float y= float.Parse(txty.Text);
float result = x- y;
txtresult.Text = result.ToString();

By using this code,not getting exact result

e.g.

x =250.60

y=250.30

above code gives result= 0.3000031 whereas desired result is 0.30

how to get the desired result

thanks in advance

Posted one year ago

This way

float x = float.Parse(txtx.Text);
float y= float.Parse(txty.Text);
float result = x- y;
txtresult.Text = result.ToString("N2");