C# 中长整型到十进制的转换
我有一个值存储在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您所做的是将文件大小除以整数,结果是整数,而不是小数。剩下的任何部分都将被砍掉。
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.
我没有在任何地方看到
fileSize
声明 - 但我会假设它很长。因此,fileSize / (1024 * 1024)
被强制设置为一个不保留任何小数位的长值,因此您会得到类似以下内容的结果:它将没有任何小数位。首先将除法转换为双精度(或其他小数),然后再将其传递给
Convert.ToDecimal
I don't see
fileSize
declared anywhere - but Im going to assume it's a long. SofileSize / (1024 * 1024)
is forced up to a long value which doesn't hold any decimal places so you get something like:Which won't have any decimal places. Convert the division to a double (or some other decimal) first before passing it to
Convert.ToDecimal
也许有点晚了,但在这里您或多或少拥有文件大小瓦特等所需的一切
以下是一些输出:
Maybe a little late but here you have more or less everything you need for filesizes watts etc
Here is some output:
这是我用来显示文件大小的函数
here is a function i use to display file size