English 中文(简体)
2. 双重过失。 格式相同
原标题:Rounding error in double vs string.Format the same number

I have a double which has actual value (as seen from the debugger) val = 1.5530000000000002

然而,如果我将其印成string.Format("{0}”, val),我就拿到“1.553”这一现实价值,我正在研究。

<代码>1.553000 000 0002<>>>> /code>源自一些目录APIC和I cant的改动。 但我需要1.553。

what shall I do?

请指出: 我收到了数千份。 I can tford/strong> tinkering with Math category or converting them listed and Back any consolations?

最佳回答

我决定予以扩大。 我可以这样做,因为我知道人数很多。

int real = (int)(dbl * multiplier);

我确信,单凭I dont有x128 CPU和记忆巴士,精子就会放慢。 采用数学是前所未有的问题。

sorry for the blur with casting to float earlier.

http://www.ohchr.org。 OK。 因此,我说,SO现在不再自由。 每一个问题现在都需要注意,因为时间上代价高昂...... 这就是我为什么不使用Decimals和Math的说法。

class Program
{
    static void Main(string[] args)
    {
        Start();
        Console.ReadLine();                
    }

    static int TESTS = 10;
    static int LOOPS = 50000;

    static double d1 = 1.5530000000000002;
    static double d2 = 1.5531;

    static double dResult;
    static int iResult;
    static decimal cResult;

    static void Start()
    {
        // actual test
        for (int x = 0; x < TESTS; x++)
        {
            Stopwatch sw = new Stopwatch();

            long tick1, tick2, tick3;
            sw.Start();
            for (int j = 0; j < LOOPS; j++)
            {
                dResult = Math.Round(d1 / 2.0, 4);
                dResult = Math.Round(d2 / 2.0, 4);
            }
            sw.Stop();

            tick1 = sw.ElapsedTicks;

            sw.Restart();
            for (int j = 0; j < LOOPS; j++)
            {
                iResult = (int)(d1 / 2.0 * 10000.0);
                iResult = (int)(d2 / 2.0 * 10000.0);
            }
            sw.Stop();

            tick2 = sw.ElapsedTicks;

            sw.Restart();
            for (int j = 0; j < LOOPS; j++)
            {
                cResult = decimal.Round((decimal)d1, 4);
                cResult = decimal.Round((decimal)d2, 4);
            }
            sw.Stop();

            tick3 = sw.ElapsedTicks;

            Console.WriteLine("Math {0} Int {1} Decimal {2}", tick1, tick2, tick3);
        }
    }
}

第一部分是数学,其次是我打算使用(转而使用)的,第三是 Dec。

www.un.org/Depts/DGACM/index_spanish.htm 我的表7结果如下:12K标准、700个标准、35K标准。

问题回答

问题是,<代码>杜布尔是一个浮动点值,其确切位置为1.553000000000<>。 <代码>decimal 保持这种准确价值更合适。

如果您不能将<代码>double改为decimal,则你别无选择,只能用数学来解决这个问题。 在这种情况下,你需要将<条码>杜布莱转换成<条码>dec/条码>,然后按照你的愿望进行数学调整。

在这方面,你可以轻松地这样做:

double value = 1.5530000000000002;
decimal rounded = decimal.Round((decimal)value, 3);

<代码>Math类比能做数千次。 因此,你们都需要做的是<代码>Math.Floor(val, 3)或Math.Round(val, 3)

从根本上说,你需要使用一种精度数据类型,而不是双重数据。 下降可以储存准确的数值,其范围是精确的;双重不能,因此所观察到的行为。

你们是否必须使用双重标准? 如不使用<代码>Decimal.Round(val, 3),例如。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...