I'm writing an application where I need to handle mm and inches. I've written some simple code to scale between the two units of measure and I also have some code to take a decimal number and convert this to a fraction. However when I split the whole number from the decimal using Math.Floor I find I do not always get the expected value returned. I've attached some code to demonstrate the problem. I expect the final result to be 6 but I get 5. I'm struggling to understand why. Any ideas?
// This is a .Net 4.5 console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double ValA = 6;
double ValB = 0;
double ValC = 0;
double ValD = 0;
ValB = ValA * 25.4;
ValC = ValB / 25.4;
ValD = Math.Floor(ValC);
Console.WriteLine(ValA.ToString());
Console.WriteLine(ValB.ToString());
Console.WriteLine(ValC.ToString()); // This returns 6 as expected
Console.WriteLine(ValD.ToString()); // But this returns 5.
Console.ReadKey();
}
}
}