Round Float Number to two Decimal Places in C#

Last Reply 9 months ago By Mudassar

Posted 9 months 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

You are viewing reply posted by: Mudassar 9 months ago.
Posted 9 months ago

This way

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