Round Float Number to two Decimal Places in C#

ashishk
 
on Apr 09, 2018 03:42 AM
2461 Views

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

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Apr 09, 2018 04:30 AM

This way

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