在C#中将长整型转换为十进制

12

我有一个存储在变量中的值,其类型为"long"。

long fileSizeInBytes = FileUploadControl.FileContent.Length;
Decimal fileSizeInMB = Convert.ToDecimal(fileSizeInBytes / (1024 * 1024));

我想将fileSizeInBytes转换为四舍五入到2位小数的十进制数(例如1.74、2.45、3.51)。但是我无法得到所需的结果。我只能得到一个没有小数位的个位数结果。有人可以帮助我吗?

提前致谢。


long 只是一个大整数,它没有小数点后面的内容。给出一个你得到的例子和你期望得到的结果,我认为你把 long 和另一种类型混淆了。 - Milimetric
@Milimetric 根据 robbrit 的建议,我尝试了他所建议的方法。例如,如果我将 20364702/(1024.0m * 1024.0m) 相除,得到的结果是:19.4212932586669921875M。但我希望结果为 19.42,即保留两位小数。我该如何做到这一点? - Sreedhar Danturthi
4个回答

22
Decimal fileSizeInMB = Convert.ToDecimal(fileSize) / (1024.0m * 1024.0m);

你所做的是将文件大小除以一个整数,这将得到一个整数而不是小数。任何余数都将被舍去。


1
@但我希望结果四舍五入到小数点后两位。如果我将20364702除以(1024.0m * 1024.0m),我得到的结果是:19.4212932586669921875M,但我想要的结果是19.42。我该怎么做? - Sreedhar Danturthi
3
你可以使用 Math.Round(fileSizeInMB, 2) 进行四舍五入保留两位小数,这是一种方法。 - anishMarokey

2

我没看到任何地方声明了fileSize,但我会假设它是一个长整型。 因此,fileSize / (1024 * 1024) 被强制转换为一个长整型值,该值不保留任何小数位,因此你会得到类似于:

Convert.ToDecimal(someLongValue)

在传递给 Convert.ToDecimal 之前,将除法转换为双精度(或其他小数位数)以避免出现任何小数位。


2
也许有点晚了,但是这里有你所需的关于文件大小、瓦特等方面的一切。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApplication1
{ 

    class Program
    {

        /// <summary>
        /// Formats the given size to the order of magniture given
        /// Order is 1 for KB, 2 for MB etc up to 8, after that you get exponents for the same notations
        /// </summary>
        /// <param name="size">The total size in bytes</param>
        /// <param name="order">+1 for each 1024 B,M,... 0 for nothing</param>
        /// <param name="unit">Usually you will want B for bytes denotation, but maybe "bit" or "bi" or W for watt</param>
        /// <param name="decimal_places">Number of desired decimal places</param>
        /// <param name="add_space">Separate KB MB etc from the number with a space?</param>
        /// <returns>Formatted size</returns>
        public static string FormatSize(string unit, double size, int order, int decimal_places, bool add_space) {

            string[] suffixes = new string[] {"", "K","M","G","T","P","E","Z","Y"};

            int exponent = order - 8 > 0 ? order - 8 : 0;
            order -= exponent;

            string suffix = suffixes[order];

            while (order > 0) {
                size /= 1024;
                order--;
            }

            string sDecimals = new String('0', decimal_places);
            string sExponent = exponent != 0 ? "E" + exponent : "";
            string dot = decimal_places > 0 ? "." : "";

            return size.ToString("#,##0" + dot + sDecimals + sExponent) + (add_space ? " " : "") + suffix + unit;
        }

        public static void Main(string[] Args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            string sz;
            sz = FormatSize("B", 1024, 1, 0, false);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024*1024 + 512, 1, 0, true);
            Console.WriteLine(sz);
            sz = FormatSize("W", 1024 * 1024 + 512, 1, 2, true);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024 * 1024 + 512, 2, 2, true);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2, 3, 0, false);
            Console.WriteLine(sz);
            sz = FormatSize("bit", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2, 3, 1, false);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 2, false);
            Console.WriteLine(sz);
            sz = FormatSize("Ω", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 1, false);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 10000000, 3, 2, false);
            Console.WriteLine(sz);
            sz = FormatSize("B", 1024 * 1024 * 1024 + 1024 * 1024 * 1024 / 2 - 1, 3, 0, false);
            Console.WriteLine(sz);
            sz = FormatSize("bit", 1208925819614629174706176f, 9, 2, true);
            Console.WriteLine(sz);
        }
    }
}

这里是一些输出:


1KB
1,025 KB
1,024.50 KW
1.00 MB
2GB
1.5Gbit
1.50GB
1.5GΩ
1.49GB
1GB
1.00E1 Ybit

1
这是一个我用来显示文件大小的函数。
//---------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Formats from bytes to KB,MB,GB,TB 
        /// </summary>
        /// <param name="number">Bytes to format</param>
        /// <returns></returns>
        public static string AutoFileSize(long number)
        {
            double tmp = number;
            string suffix = " B ";
            if (tmp > 1024) { tmp = tmp / 1024; suffix = " KB"; }
            if (tmp > 1024) { tmp = tmp / 1024; suffix = " MB"; }
            if (tmp > 1024) { tmp = tmp / 1024; suffix = " GB"; }
            if (tmp > 1024) { tmp = tmp / 1024; suffix = " TB"; }
            return tmp.ToString("n") + suffix;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接