小编Chr*_*sUK的帖子

使用Math.Floor的意外结果

我正在编写一个应用程序,我需要处理毫米和英寸.我写了一些简单的代码来缩放两个度量单位之间我也有一些代码来取一个十进制数并将其转换为分数.但是,当我使用Math.Floor从小数中分割整数时,我发现我并不总是得到返回的预期值.我附上了一些代码来证明这个问题.我希望最终的结果是6但我得到5.我很难理解为什么.有任何想法吗?

// 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# math

-1
推荐指数
1
解决办法
97
查看次数

标签 统计

c# ×1

math ×1