截断数字以获得特定精度的最有效方法是什么?

发布于 2024-09-02 05:15:29 字数 28 浏览 4 评论 0原文

截断数字以获得特定精度的最有效方法是什么?

What is the most efficient way to truncate a number for a specific accuracy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-09-09 05:15:29

In a DateTime, Milliseconds are always comprised between 0 and 999 so you don't have anything to do.

空城之時有危險 2024-09-09 05:15:29
int ms = Convert.ToInt32(
             Convert.ToString(DateTime.Now.Millisecond).Substring(0, 3));

double Length = Math.Pow(10, (DateTime.Now.Millisecond.ToString().Length - 3));

double Truncate = Math.Truncate((double)DateTime.Now.Millisecond / Length);

编辑:

在我将发布的代码上运行下面的代码后,由于变量的重用,双倍方法效果很好。在 5,000,000 次 DateTime.Now 的迭代中(其中许多检查都会被跳过),SubString() 方法花费了 9598 毫秒,Double 方法花费了 6754 毫秒。

编辑#2:在 * 1000 中编辑到测试中以确保迭代正在运行。

用于测试的代码如下:

        Stopwatch stop = new Stopwatch();
        stop.Start();

        for (int i = 0; i < 5000000; i++)
        {
            int MSNow = DateTime.Now.Millisecond * 1000;

            if (MSNow.ToString().Length > 2)
            {
                int ms = Convert.ToInt32(
                    Convert.ToString(MSNow).Substring(0, 3));
            }
        }

        stop.Stop();

        Console.WriteLine(stop.ElapsedMilliseconds);

        stop = new Stopwatch();
        stop.Start();

        for (int i = 0; i < 5000000; i++)
        {
            int MSNow = DateTime.Now.Millisecond * 1000;
            int lengthMS = MSNow.ToString().Length;

            if (lengthMS > 2)
            {
                double Length = Math.Pow(10, (lengthMS - 3));
                double Truncate = Math.Truncate((double)MSNow / Length);
            }
        }

        stop.Stop();

        Console.Write(stop.ElapsedMilliseconds);

        Console.ReadKey();
int ms = Convert.ToInt32(
             Convert.ToString(DateTime.Now.Millisecond).Substring(0, 3));

or

double Length = Math.Pow(10, (DateTime.Now.Millisecond.ToString().Length - 3));

double Truncate = Math.Truncate((double)DateTime.Now.Millisecond / Length);

EDIT:

After running both the below on the code I will post, the double method works well due to reuse of variables. Over an iteration of 5,000,000 DateTime.Now's (in which many will be skipped by both checks), the SubString() method took 9598ms, and the Double method took 6754ms.

EDIT#2: Edited in * 1000 into tests to make sure the iterations are running.

Code used to test as follows:

        Stopwatch stop = new Stopwatch();
        stop.Start();

        for (int i = 0; i < 5000000; i++)
        {
            int MSNow = DateTime.Now.Millisecond * 1000;

            if (MSNow.ToString().Length > 2)
            {
                int ms = Convert.ToInt32(
                    Convert.ToString(MSNow).Substring(0, 3));
            }
        }

        stop.Stop();

        Console.WriteLine(stop.ElapsedMilliseconds);

        stop = new Stopwatch();
        stop.Start();

        for (int i = 0; i < 5000000; i++)
        {
            int MSNow = DateTime.Now.Millisecond * 1000;
            int lengthMS = MSNow.ToString().Length;

            if (lengthMS > 2)
            {
                double Length = Math.Pow(10, (lengthMS - 3));
                double Truncate = Math.Truncate((double)MSNow / Length);
            }
        }

        stop.Stop();

        Console.Write(stop.ElapsedMilliseconds);

        Console.ReadKey();
情愿 2024-09-09 05:15:29
Math.Floor(num * Math.Pow(10, x) + 0.5) / Math.Pow(10, x)

你的精度在哪里

Math.Floor(num * Math.Pow(10, x) + 0.5) / Math.Pow(10, x)

Where x your precision

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文