C# 中长整型到十进制的转换

发布于 2024-11-04 06:17:40 字数 305 浏览 2 评论 0原文

我有一个值存储在“long”类型的变量中。

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

我想将 fileSizeInBytes 转换为小数,四舍五入到小数点后两位(例如:1.74、2.45、3.51),但我无法获得所需的结果。结果我只得到一位数,没有小数位。有人可以帮我吗?

感谢期待

I have a value stored in a variable which is of type "long".

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

I want to convert the fileSizeInBytes to decimal number rounded up to 2 decimal places (like these: 1.74, 2.45, 3.51) But i'm not able to get the required result. I'm only getting single digit with no decimal places as result. Can some one help me with that ??.

Thanks in anticipation

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

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

发布评论

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

评论(4

黑色毁心梦 2024-11-11 06:17:40
Decimal fileSizeInMB = Convert.ToDecimal(fileSize) / (1024.0m * 1024.0m);

您所做的是将文件大小除以整数,结果是整数,而不是小数。剩下的任何部分都将被砍掉。

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

What you're doing is dividing the filesize by an integer, which results in an integer, not a decimal. Any remainder left over will be chopped off.

策马西风 2024-11-11 06:17:40

我没有在任何地方看到 fileSize 声明 - 但我会假设它很长。因此,fileSize / (1024 * 1024) 被强制设置为一个不保留任何小数位的长值,因此您会得到类似以下内容的结果:

Convert.ToDecimal(someLongValue)

它将没有任何小数位。首先将除法转换为双精度(或其他小数),然后再将其传递给 Convert.ToDecimal

I don't see fileSize declared anywhere - but Im going to assume it's a long. So fileSize / (1024 * 1024) is forced up to a long value which doesn't hold any decimal places so you get something like:

Convert.ToDecimal(someLongValue)

Which won't have any decimal places. Convert the division to a double (or some other decimal) first before passing it to Convert.ToDecimal

z祗昰~ 2024-11-11 06:17:40

也许有点晚了,但在这里您或多或少拥有文件大小瓦特等所需的一切

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

Maybe a little late but here you have more or less everything you need for filesizes watts etc

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);
        }
    }
}

Here is some output:

1KB
1,025 KB
1,024.50 KW
1.00 MB
2GB
1.5Gbit
1.50GB
1.5GΩ
1.49GB
1GB
1.00E1 Ybit
没有你我更好 2024-11-11 06:17:40

这是我用来显示文件大小的函数

//---------------------------------------------------------------------------------------------------------------------------------
        /// <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;
        }

here is a function i use to display file size

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