各种格式化文件大小为字符串的函数FormatFileSize

asp的:

Public Function formatdsize(dsize)
    if dsize>=1073741824 then
         formatdsize=Formatnumber(dsize/1073741824,2) & " GB"
    elseif dsize>=1048576 then
          formatdsize=Formatnumber(dsize/1048576,2) & " MB"
    elseif dsize>=1024 then
         formatdsize=Formatnumber(dsize/1024,2) & " KB"
    else
         formatdsize=dsize & "B"
  end if
End Function

有用的思想:

ASP中file.filesize>2000000是限制到多少K呀?

<%dim sc_dx'//定义上传文件大小,单位KB
sc_dx=30
x.SetFileSize(sc_dx*1024) '最大文件大小
%>

这个sc_dx就是30KB了,这个方法可以用到以后的vb.net 上传图片到sql server里面。

 

VB.Net的:from: http://www.devx.com/tips/Tip/21185

Convert the Filesize in a Number into String Format
This code converts the filesize in a number to string format—for instance, 1000 Bytes, 1 KB, etc.
Option Explicit

' Add a module and copy paste this function.
' To test this function, call this function from Immediate window by passing
' file size to it.  e.g. ?GetFileSizeString(1000), GetFileSizeString(10000)

Private Declare Function StrFormatByteSize Lib "shlwapi.dll" Alias "StrFormatByteSizeA" _
(ByVal dw As Long, ByVal szBuf As String, ByVal uiBufSize As Long) As Long

Private Function GetFileSizeString(ByVal lngFileSize As Long) As String
    Dim strBuff As String

    strBuff = Space$(100)
    StrFormatByteSize lngFileSize, strBuff, Len(strBuff)

    GetFileSizeString = Replace(Trim$(strBuff), vbNullChar, "")
End Function
------------------------
Module Module1
    Sub Main()
        Dim i As Long
'随便找一个数字来测试
        i = 50850000000000000
        Console.WriteLine("--{0}--", i)
        Console.WriteLine("1.  " & FormatFileSize(i, , False))
        Console.WriteLine("2.  " & sFormatFileSize(i))
        Console.WriteLine("3.  " & nFormatFileSize(i))
        Console.ReadLine()
    End Sub
'--------这个是老外写的。
    Function FormatFileSize(ByVal FileSize As Decimal, Optional ByVal DecimalDigits As Byte = 1, Optional ByVal Bytes As Boolean = True) As String
        If FileSize = 0 Then Return "0" & IIf(Bytes, "bytes", "bits") ' Prevent overflow
        Dim Power As Integer = Math.Min(Math.Floor(Math.Log(Math.Ceiling(Math.Abs(FileSize)), 1024)), 8)
        Dim Str As String = IIf(Power = 0, IIf(Bytes, "bytes", "bits"), New String() {"KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}(Power - 1))
        Return Format(FileSize / (1024 ^ Power), "#." & StrDup(DecimalDigits, "0")) & " " & IIf(Bytes, Str, LCase(Str))
    End Function
'改成这个
    Function sFormatFileSize(ByVal FileSize As Decimal, Optional ByVal DecimalDigits As Byte = 1) As String
        If FileSize = 0 Then Return "0"
        Dim Power As Integer = Math.Min(Math.Floor(Math.Log(Math.Ceiling(Math.Abs(FileSize)), 1024)), 8)
        Dim Str As String = New String() {"KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}(Power - 1)
        Return Format(FileSize / (1024 ^ Power), "#." & StrDup(DecimalDigits, "0")) & " " & Str
    End Function
'修改版本,毕竟现在硬盘刚刚出TB级别的,所以TB应该就够用的了。
    Function nFormatFileSize(ByVal FileSize As Decimal, Optional ByVal DecimalDigits As Byte = 1) As String
        If FileSize = 0 Then Return "0"
        Dim Power As Integer = Math.Min(Math.Floor(Math.Log(Math.Ceiling(Math.Abs(FileSize)), 1024)), 4)
        Dim Str As String = New String() {"KB", "MB", "GB", "TB"}(Power - 1)
        Return Format(FileSize / (1024 ^ Power), "#." & StrDup(DecimalDigits, "0")) & " " & Str
    End Function
End Module

 

----------------------

 

c#的:

Read more: "An example on how to format file size in C#" - http://www.waynejohn.com/post/2008/04/04/An-example-on-how-to-format-file-size-in-C.aspx#ixzz0ArSxzGDB

 

public class FileHelper
{
    private static readonly long kilobyte = 1024;
    private static readonly long megabyte = 1024 * kilobyte;
    private static readonly long gigabyte = 1024 * megabyte;
    private static readonly long terabyte = 1024 * gigabyte;
    public static string ToByteString(long bytes)
    {
        if (bytes > terabyte)
            return (bytes / terabyte).ToString("0.00 TB");
        else if (bytes > gigabyte) return (bytes / gigabyte).ToString("0.00 GB");
        else if (bytes > megabyte) return (bytes / megabyte).ToString("0.00 MB");
        else if (bytes > kilobyte) return (bytes / kilobyte).ToString("0.00 KB");
        else return bytes + " Bytes";
    }
}

 

或者调用API函数:

StrFormatByteSize (StrFormatByteSizeA,StrFormatByteSizeW or StrFormatByteSize64)

来源from:http://bytes.com/groups/net-c/515107-there-any-file-size-format-function-c

 

private string formatsizekb(double dsize)
{
const int iKB = 1024;
const long iMB = 1048576;
const long iGB= 1073741824;

if (dsize < iKB)
return string.format("{0} bytes", dsize);

if (dsize >= iKB && dsize < iMB)
return string.format("{0} KB", dsize/iKB);

if (dsize >= iMB && dsize < iGB)
return string.format("{0} MB", dsize/iMB);

if (dsize >= iGB )
return string.format("{0} GB", dsize/iGB);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值