Reputation: 1550
Can anyone please help me understand that why output for float is rounding off decimal ?
Code -
static void Main(string[] args)
{
float f = (float)Math.Round(Convert.ToDouble("270825.27000000000000"), 2);
double d = Math.Round(Convert.ToDouble("270825.27000000000000"), 2);
Console.WriteLine("Float - " + f.ToString());
Console.WriteLine("Double - " + d.ToString());
Console.Read();
}
Output -
Thanks in Advance.
Upvotes: 0
Views: 1637
Reputation: 9473
you are casting double to float, so basicly you have:
casting
float f = (float)Math.Round(float.Parse("270825.27000000000000"), 2);
double d = Math.Round(Convert.ToDouble("270825.27000000000000"), 2);
Console.WriteLine("Float - " + f.ToString());
Console.WriteLine("Double - " + d.ToString());
results are:
Float - 270825.3
Double - 270825.27
so use decimal
decimal f = (decimal)Math.Round(decimal.Parse("270825.27000000000000"), 2);
Console.WriteLine("Decimal - " + f.ToString());
270825.27
Upvotes: 1