怎样在Delphi中怎样格式化硬盘盘,请教各位高手

硬盘格式化代格(delphi) - 孙忠好 - 博客园
随笔 - 1, 文章 - 0, 评论 - 0, 引用 - 0
以下是我用delphi写的硬盘分区代码
& IOCTL_DISK_BASE = $;
& IOCTL_DISK_GET_DRIVE_GEOMETRY = $;
& IOCTL_DISK_GET_PARTITION_INFO = $;
& IOCTL_DISK_SET_PARTITION_INFO = $;
& IOCTL_DISK_GET_DRIVE_LAYOUT = $0007400C;
& IOCTL_DISK_SET_DRIVE_LAYOUT = $;
& IOCTL_DISK_VERIFY = $;
& IOCTL_DISK_FORMAT_TRACKS = $;
& IOCTL_DISK_REASSIGN_BLOCKS = $0007C01C;
& IOCTL_DISK_PERFORMANCE = $;
& IOCTL_DISK_IS_WRITABLE = $;
& IOCTL_DISK_LOGGING = $;
& IOCTL_DISK_FORMAT_TRACKS_EX = $0007C02C;
& IOCTL_DISK_HISTOGRAM_STRUCTURE = $;
& IOCTL_DISK_HISTOGRAM_DATA = $;
& IOCTL_DISK_HISTOGRAM_RESET = $;
& IOCTL_DISK_REQUEST_STRUCTURE = $0007003C;
& IOCTL_DISK_REQUEST_DATA = $;
& IOCTL_DISK_PERFORMANCE_OFF = $;
& IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = $;
& IOCTL_VOLUME_IS_CLUSTERED = $;
& _MEDIA_TYPE=(DDS_4mm = $20,MiniQic,
&&& Travan,
&&& MP_8mm,
&&& AME_8mm,&&&&&&&&&&&&&&&&&& // Tape - 8mm Exabyte Advanced Metal Evap
&&& AIT1_8mm,&&&&&&&&&&&&&&&&& // Tape - 8mm Sony AIT
&&& DLT,&&&&&&&&&&&&&&&&&&&&&& // Tape - DLT Compact IIIxt, IV
&&& NCTP,&&&&&&&&&&&&&&&&&&&&& // Tape - Philips NCTP
&&& IBM_3480,&&&&&&&&&&&&&&&&& // Tape - IBM 3480
&&& IBM_3490E,&&&&&&&&&&&&&&&& // Tape - IBM 3490E
&&& IBM_Magstar_3590,&&&&&&&&& // Tape - IBM Magstar 3590
&&& IBM_Magstar_MP,&&&&&&&&&&& // Tape - IBM Magstar MP
&&& STK_DATA_D3,&&&&&&&&&&&&&& // Tape - STK Data D3
&&& SONY_DTF,&&&&&&&&&&&&&&&&& // Tape - Sony DTF
&&& DV_6mm,&&&&&&&&&&&&&&&&&&& // Tape - 6mm Digital Video
&&& DMI,&&&&&&&&&&&&&&&&&&&&&& // Tape - Exabyte DMI and compatibles
&&& SONY_D2,&&&&&&&&&&&&&&&&&& // Tape - Sony D2S and D2L
&&& CLEANER_CARTRIDGE,&&&&&&&& // Cleaner - All Drive types that support Drive Cleaners
&&& CD_ROM,&&&&&&&&&&&&&&&&&&& // Opt_Disk - CD
&&& CD_R,&&&&&&&&&&&&&&&&&&&&& // Opt_Disk - CD-Recordable (Write Once)
&&& CD_RW,&&&&&&&&&&&&&&&&&&&& // Opt_Disk - CD-Rewriteable
&&& DVD_ROM,&&&&&&&&&&&&&&&&&& // Opt_Disk - DVD-ROM
&&& DVD_R,&&&&&&&&&&&&&&&&&&&& // Opt_Disk - DVD-Recordable (Write Once)
&&& DVD_RW,&&&&&&&&&&&&&&&&&&& // Opt_Disk - DVD-Rewriteable
&&& MO_3_RW,&&&&&&&&&&&&&&&&&& // Opt_Disk - 3.5" Rewriteable MO Disk
&&& MO_5_WO,&&&&&&&&&&&&&&&&&& // Opt_Disk - MO 5.25" Write Once
&&& MO_5_RW,&&&&&&&&&&&&&&&&&& // Opt_Disk - MO 5.25" Rewriteable (not LIMDOW)
&&& MO_5_LIMDOW,&&&&&&&&&&&&&& // Opt_Disk - MO 5.25" Rewriteable (LIMDOW)
&&& PC_5_WO,&&&&&&&&&&&&&&&&&& // Opt_Disk - Phase Change 5.25" Write Once Optical
&&& PC_5_RW,&&&&&&&&&&&&&&&&&& // Opt_Disk - Phase Change 5.25" Rewriteable
&&& PD_5_RW,&&&&&&&&&&&&&&&&&& // Opt_Disk - PhaseChange Dual Rewriteable
&&& ABL_5_WO,&&&&&&&&&&&&&&&&& // Opt_Disk - Ablative 5.25" Write Once Optical
&&& PINNACLE_APEX_5_RW,&&&&&&& // Opt_Disk - Pinnacle Apex 4.6GB Rewriteable Optical
&&& SONY_12_WO,&&&&&&&&&&&&&&& // Opt_Disk - Sony 12" Write Once
&&& PHILIPS_12_WO,&&&&&&&&&&&& // Opt_Disk - Philips/LMS 12" Write Once
&&& HITACHI_12_WO,&&&&&&&&&&&& // Opt_Disk - Hitachi 12" Write Once
&&& CYGNET_12_WO,&&&&&&&&&&&&& // Opt_Disk - Cygnet/ATG 12" Write Once
&&& KODAK_14_WO,&&&&&&&&&&&&&& // Opt_Disk - Kodak 14" Write Once
&&& MO_NFR_525,&&&&&&&&&&&&&&& // Opt_Disk - Near Field Recording (Terastor)
&&& NIKON_12_RW,&&&&&&&&&&&&&& // Opt_Disk - Nikon 12" Rewriteable
&&& IOMEGA_ZIP,&&&&&&&&&&&&&&& // Mag_Disk - Iomega Zip
&&& IOMEGA_JAZ,&&&&&&&&&&&&&&& // Mag_Disk - Iomega Jaz
&&& SYQUEST_EZ135,&&&&&&&&&&&& // Mag_Disk - Syquest EZ135
&&& SYQUEST_EZFLYER,&&&&&&&&&& // Mag_Disk - Syquest EzFlyer
&&& SYQUEST_SYJET,&&&&&&&&&&&& // Mag_Disk - Syquest SyJet
&&& AVATAR_F2,&&&&&&&&&&&&&&&& // Mag_Disk - 2.5" Floppy
&&& MP2_8mm,&&&&&&&&&&&&&&&&&& // Tape - 8mm Hitachi
&&& DST_S,&&&&&&&&&&&&&&&&&&&& // Ampex DST Small Tapes
&&& DST_M,&&&&&&&&&&&&&&&&&&&& // Ampex DST Medium Tapes
&&& DST_L,&&&&&&&&&&&&&&&&&&&& // Ampex DST Large Tapes
&&& VXATape_1,&&&&&&&&&&&&&&&& // Ecrix 8mm Tape
&&& VXATape_2,&&&&&&&&&&&&&&&& // Ecrix 8mm Tape
&&& STK_9840,&&&&&&&&&&&&&&&&& // STK 9840
&&& LTO_Ultrium,&&&&&&&&&&&&&& // IBM, HP, Seagate LTO Ultrium
&&& LTO_Accelis,&&&&&&&&&&&&&& // IBM, HP, Seagate LTO Accelis
&&& DVD_RAM,&&&&&&&&&&&&&&&&&& // Opt_Disk - DVD-RAM
&&& AIT_8mm,&&&&&&&&&&&&&&&&&& // AIT2 or higher
&&& ADR_1,&&&&&&&&&&&&&&&&&&&& // OnStream ADR Mediatypes
& MEDIA_TYPE = _MEDIA_TYPE;
& _DISK_GEOMETRY = Record
&&& Cylinders: LARGE_INTEGER;
&&& MediaType: MEDIA_TYPE;
&&& TracksPerCylinder: DWORD;
&&& SectorsPerTrack: DWORD;
&&& BytesPerSector: DWORD;
& DISK_GEOMETRY = _DISK_GEOMETRY;
& PDISK_GEOMETRY = ^_DISK_GEOMETRY;
& _DISK_EXTENT = Record
&&& DiskNumber: DWORD;
&&& StartingOffset: LARGE_INTEGER;
&&& ExtentLength: LARGE_INTEGER;
& DISK_EXTENT = _DISK_EXTENT;
& PDISK_EXTENT = ^_DISK_EXTENT;
& _VOLUME_DISK_EXTENTS = Record
&&& NumberOfDiskExtents: DWORD;
&&& Extents: DISK_EXTENT;
& VOLUME_DISK_EXTENTS = _VOLUME_DISK_EXTENTS;
& PVOLUME_DISK_EXTENTS = ^_VOLUME_DISK_EXTENTS;
Function fDisk(DiskID: Integer):B
& hDevice: TH
& Tracks,Sectors,Cylinder:DWORD;
& hdevicename: AnsiS
& geometry: DISK_GEOMETRY;
& labOffset: OVERLAPPED;
& count: DWORD;
& dwLen: DWORD;
& pBuffer: array[0..511]
& PhysicalSectors,PhysicalTracks,PhysicalCylinders,PhysicalSectorsTmp: DWORD;
& CylindersOffet,TracksOffet,SectorOffet:DWORD;
& CylinderTemp: DWORD;&& //临时保留了柱面的高2位+扇区号的低6位+柱面的低8位
& StartLogicSector: DWORD;
& SectorsSum: DWORD;
& sei: SHELLEXECUTEINFO;
& szFile: AnsiS
& hHnd: TH
& Tracks := 1;&&&&&&& //起始磁头
& Sectors := 1;&&&&&& //起始扇区
& Cylinder := 0;&&&& //起始柱面
& hdevicename := Format('\\.\PHYSICALDRIVE%d',[DiskID]);
& hDevice := CreateFile(pchar(hdevicename),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,nil,OPEN_EXISTING,0,0);
& if hDevice=INVALID_HANDLE_VALUE then
&&& Result := F
& if (not DeviceIoControl(hDevice,IOCTL_DISK_GET_DRIVE_GEOMETRY,nil,0,@geometry,sizeof(geometry),count,nil)) then
&&& Result := F
&&& CloseHandle(hDevice);
& FillChar(pBuffer,sizeof(pBuffer),#0);
& //读取第一扇区
& ZeroMemory(@labOffset,sizeof(labOffset));
& labOffset.Offset := 1;
& ReadFile(hDevice, pBuffer, 512, dwLen, @labOffset);
& //0~445字节为引导记录,446~509,为分区表信息占64个字节,510~511为结束标志55AA,占两个字节
& pBuffer[446] := $00;&& //活动分区
& PhysicalSectorsTmp := (MaxFileSize+511) div geometry.BytesPerS& //物理区理论占用的扇区数,从下一磁头开始
& PhysicalCylinders := PhysicalSectorsTmp div (geometry.SectorsPerTrack * geometry.TracksPerCylinder)+1;&& //物理占用的柱面数
& PhysicalTracks := (PhysicalSectorsTmp mod (geometry.SectorsPerTrack * geometry.TracksPerCylinder)) div geometry.SectorsPerT&& //物理区占用的磁头数
& PhysicalSectors := geometry.SectorsPerTrack * PhysicalCylinders * geometry.TracksPerC&& //物理区实际上占用的扇区数
& Tracks := Tracks+PhysicalT&&&&&&& //起始磁头
& Sectors := 1;&&&&&& //起始扇区
& //Cylinder = Cylinder + (PhysicalSectorsTmp-1)/(geometry.TracksPerCylinder * geometry.SectorsPerTrack) + 1;& //起始柱面
& Cylinder := Cylinder+PhysicalC&&&& //起始柱面
& //起始地址
& pBuffer[447] := 0;//磁头号
& CylinderTemp := ((((Cylinder shr 2) and $C0) or (Sectors and $3F)) shl 8) or (Cylinder and $00FF);
& pBuffer[448] := CylinderTemp shr 8;&& //右移8位取柱面的高2位+扇区号的低6位
& pBuffer[449] := CylinderT
& //磁盘格式
& pBuffer[450] := $0C;&& //Fat32格式
& //结束地址
& CylindersOffet := geometry.Cylinders.LowPart-1;&&& //结束柱面号
& //CylindersOffet = 1023;
& TracksOffet := geometry.TracksPerCylinder-1;&&&& //结束磁头号
& SectorOffet := geometry.SectorsPerT&& //结束扇区号
& pBuffer[451] := TracksO
& CylinderTemp := ((((CylindersOffet shr 2) and $C0) or (SectorOffet and $3F)) shl 8) or (CylindersOffet and $00FF);
& pBuffer[452] := CylinderTemp shr 8;
& pBuffer[453] := CylinderT
& //设置逻辑扇区号
& StartLogicSector := PhysicalS
& //StartLogicSector = 63;
& pBuffer[454] := StartLogicS
& pBuffer[455] := StartLogicSector shr 8;
& pBuffer[456] := StartLogicSector shr 16;
& pBuffer[457] := StartLogicSector shr 24;
& //总扇区数
& SectorsSum := geometry.Cylinders.LowPart*geometry.TracksPerCylinder*geometry.SectorsPerT
& SectorsSum := SectorsSum - PhysicalS
& //SectorsSum = SectorsSum - 62- geometry.SectorsPerT
& pBuffer[458] := SectorsS
& pBuffer[459] := SectorsSum shr 8;
& pBuffer[460] := SectorsSum shr 16;
& pBuffer[461] := SectorsSum shr 24;
& //没别其它分区
& for i := 462 to 509 do
&&& pBuffer[i] := $0;
& //结束标志
& pBuffer[510] := $55;
& pBuffer[511] := $AA;
& str := '';
& //for(int j=0;j&512;j++)
& //& str+=IntToHex(pBuffer[j];
& //ShowMessage(str);
& //memset(&labOffset,0,sizeof(labOffset));
& FillChar(labOffset,sizeof(labOffset),0);
& labOffset.Offset := 1;
& bRet := WriteFile(hDevice, pBuffer, 512, dwLen, @labOffset); //写入分区表
& CloseHandle(hDevice);
& if (bRet)& then
&&& FillChar(sei,sizeof(sei),0);
&&& szFile := 'C:\WINNT\system32\Diskmgmt.msc';
&&& if (not FileExists(szFile) ) then
&&&&& Result := F
&&&&& MessageDlg('无法找到硬盘管理工具',mtWarning,[mbok],0);
&&& sei.cbSize := sizeof(SHELLEXECUTEINFO);
&&& sei.fMask := SEE_MASK_NOCLOSEPROCESS;
&&& sei.lpVerb := 'open'+#0;
&&& sei.lpFile := Pchar(szFile);
&&& sei.lpParameters := '';
&&& sei.nShow := SW_SHOWNORMAL;&&&&&&&&&&&&& //SW_SHOWNORMAL; //SW_SHOWDEFAULT; //SW_HIDE
&&& bRet := ShellExecuteExA(@sei);
&&& if (bRet) then
&&&&& hHnd := INVALID_HANDLE_VALUE;
&&&&& bRet := FALSE;
&&&&& for i := 0 to 100 do
&&&&& begin
&&&&&&&&& Sleep(300);
&&&&&&&&& if (not bRet) then
&&&&&&&&& begin
&&&&&&&&&&&&& hHnd := FindWindow(nil, '磁盘管理');
&&&&&&&&&&&&& bRet := SetWindowPos(hHnd, HWND_BOTTOM, 0, 0, 50, 50, SWP_HIDEWINDOW);
&&&&&&&&& Application.ProcessMessages();
&&&&& TerminateProcess(sei.hProcess,1);
&&&&& CloseHandle(sei.hProcess);
&&& Result := T
& Result := F一、Format函数的用法Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用:
首先看它的声明:function Format(const Format: const Args: array of const):事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,但并不多用,所以这里只对第一个介绍:
function Format(const Format: const Args: array of const):Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。如以下例子:Format('my name is %6s',['wind']);返回后就是my name is wind
现在来看Format参数的详细情况:Format里面可以写普通的字符串,比如'my name is' 但有些格式指令字符具有特殊意义,比如"%6s"
格式指令具有以下的形式:"%" [index ":"] ["-"] [width] ["." prec] type它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来格式化type类型的指令字符,是可选的。
先来看看type,type可以是以下字符:d 十制数,表示一个整型值u 和d一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时是一个2的32次方减去这个绝对值的数&& 如:Format('this is %u',[-2]);&& 返回的是:this is f 对应浮点数e 科学表示法,对应整型数和浮点数,&& 比如Format('this is %e',[-2.22]);&& 返回的是:this is -2.00E+000&& 等一下再说明如果将数的精度缩小g 这个只能对应浮点型,且它会将值中多余的数去掉&& 比如Format('this is %g',[02.200]);&& 返回的是:this is 2.2n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了&& Format('this is %n',[]);&& 返回的是this is 4,552.22&& 注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况&& 二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的m 钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化&& 另外它只对应于浮点值&& Format('this is %m',[9552.21]);返回:this is ¥9,552.21p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示&& 例如:&& var X:&&&& p:^&& begin&&& X:=99;&&& p:=@X;&&& Edit1.Text:=Format('this is %p',[p]);&&&& Edit1的内容是:this is s 对应字符串类型,不用多说了吧x 必须是一个整形值,以十六进制的形式返回&& Edit1.Text:=Format('this is %X',[15]);&& 返回是:this is F
类型讲述完毕,下面介绍格式化Type的指令:[index ":"] 这个要怎么表达呢,看一个例子&&&&&&&&&&&& Format('this is %d %d',[12,13]);&&&&&&&&&&&& 其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候&&&&&&&&&&&& 是这样 this is 12 13
&&&&&&&&&&&& 而如果你这样定义:&&&&&&&&&&&& Format('this is %1:d %0:d',[12,13]);&&&&&&&&&&&& 那么返回的字符串就变成了&&&&&&&&&&&& this is 13 12&&&&&&&&&&&& 现在明白了吗,[index ":"] 中的index指示Args中参数显示的&&&&&&&&&&&& 顺序
&&&&&&&&&&&& 还有一种情况,如果这样Format('%d %d %d %0:d %d', [1, 2, 3, 4])&&&&&&&&&&&& 将返回1 2 3 1 2。&&&&&&&&&&&& 如果你想返回的是1 2 3 1 4,必须这样定:&&&&&&&&&&&& Format('%d %d %d %0:d %3:d', [1, 2, 3, 4])&&&&&&&&&&&& 但用的时候要注意,索引不能超出Args中的个数,不然会引起异常&&&&&&&&&&&& 如Format('this is %2:d %0:d',[12,13]);&&&&&&&&&&&& 由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了[width] 指定将被格式化的值占的宽度,看一个例子就明白了&&&&&&&& Format('this is %4d',[12]);&&&&&&&& 输出是:this is&&& 12&&&&&&&& 这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。&&&&&&&& 如:Format('this is %1d',[12]);&&&&&&&& 输出是:this is 12["-"]&& 这个指定参数向左齐,和[width]合在一起最可以看到效果:&&&&&&& Format('this is %-4d,yes',[12]);&&&&&&& 输出是:this is 12&&& ,yes&&&&&&&&["." prec] 指定精度,对于浮点数效果最佳:&&&&&&&&&&& Format('this is %.2f',['1.1234]);&&&&&&&&&&& 输出 this is 1.12&&&&&&&&&&& Format('this is %.7f',['1.1234]);&&&&&&&&&&& 输了 this is 1.1234000
&&&&&&&&&&& 而对于整型数,如果prec比如整型的位数小,则没有效果反之比整形值的位数大,则会在整型值的前面以0补之&&&&&&&&&&& Format('this is %.7d',[1234]);&&&&&&&&&&& 输出是:this is 0001234]&&&&&&&&&&&&&&&&&&&&& 对于字符型,刚好和整型值相反,如果prec比字符串型的长度大则没有效果,反之比字符串型的长度小,则会截断尾部的字符&&&&&&&&&&& Format('this is %.2s',['1234']);&&&&&&&&&&& 输出是 this is 12&&&&&&&&&&&&&&&&&&&&&& 而上面说的这个例子:&&&&&&&&&&& Format('this is %e',[-2.22]);&&&&&&&&&&& 返回的是:this is -2.00E+000&&&&&&&&&&& 怎么去掉多余的0呢,这个就行啦&&&&&&&&&&& Format('this is %.2e',[-2.22]);二 FormatDateTime的用法他的声明为:function FormatDateTime(const Format: DateTime: TDateTime):当然和Format一样还有一种,但这里只介绍常用的第一种Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的字符串
重点来看Format参数中的指令字符c 以短时间格式显示时间,即全部是数字的表示&& FormatdateTime('c',now);&& 输出为: 9:55:40d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位&& FormatdateTime('d',now);&& 输出可能为1~31dd 和d的意义一样,但它始终是以两位来显示的&& FormatdateTime('dd',now);&&& 输出可能为01~31ddd 显示的是星期几&&& FormatdateTime('ddd',now);&&& 输出为: 星期六dddd 和ddd显示的是一样的。&&& 但上面两个如果在其他国家可能不一样。ddddd 以短时间格式显示年月日 &&&& FormatdateTime('ddddd',now);&&&& 输出为:dddddd 以长时间格式显示年月日&&&& FormatdateTime('dddddd',now); &&&& 输出为:日e/ee/eee/eeee 以相应的位数显示年&&&&& FormatdateTime('ee',now); &&&& 输出为:04&& (表示04年)m/mm/mmm/mmmm 表示月&&&&& FormatdateTime('m',now);&&&&& 输出为:8&&&&& FormatdateTime('mm',now);&&&&& 输出为&& 08&&&&& FormatdateTime('mmm',now);&&&&& 输出为&& 八月&&&&& FormatdateTime('mmmm',now); &&&&& 输出为&& 八月&&&& 和ddd/dddd 一样,在其他国家可能不同yy/yyyy 表示年&&&&& FormatdateTime('yy',now);&&&&& 输出为 04&&&&& FormatdateTime('yyyy',now);&&&&& 输出为 2004h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒t&& 以短时间格式显示时间&&&&& FormatdateTime('t',now);&&&& 输出为 10:17tt 以长时间格式显示时间&&&&& FormatdateTime('tt',now);&&&&& 输出为10:18:46ampm 以长时间格式显示上午还是下午&&&&& FormatdateTime('ttampm',now);&&&&& 输出为:10:22:57上午
大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为时间格式啦:FormatdateTime('"today is" c',now);输出为:today is
10:26:58时间中也可以加"-"或"/"来分开日期:FormatdateTime('"today is" yy-mm-dd',now);FormatdateTime('"today is" yy/mm/dd',now);输出为: today is 04-08-07也可以用":"来分开时间&&FormatdateTime('"today is" hh:nn:ss',now);输出为:today is 10:32:23
三.FormatFloat的用法
常用的声明:function FormatFloat(const Format: Value: Extended):和上面一样Format参数为格式化指令字符,Value为Extended类型为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数比如Double或者其他,则可以保存不会超出范围。
关键是看Format参数的用法0&& 这个指定相应的位数的指令。&&& 比如:FormatFloat('000.000',22.22);&&& 输出的就是022.220&&& 注意一点,如果整数部分的0的个数小于Value参数中整数的位数,则没有效果&&& 如:FormatFloat('0.00',22.22);&&& 输出的是:22.22&&& 但如果小数部分的0小于Value中小数的倍数,则会截去相应的小数和位数&&& 如:FormatFloat('0.0',22.22);&&& 输出的是:22.2&&&&&& 也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出现&&& FormatFloat('0,000.0',2222.22);&&& 输出是:2,222.2&&& 如果这样FormatFloat('000,0.0',2222.22);&&& 它的输出还是:2,222.2&&& 注意它的规律
#&& 和0的用法一样,目前我还没有测出有什么不同。&&& FormatFloat('##.##',22.22);&&& 输出是:22.00
E&& 科学表示法,看几个例子大概就明白了&&& FormatFloat('0.00E+00',2222.22);&&& 输出是 2.22E+03&&& FormatFloat('',2222.22);&&& 输出是 &&&& FormatFloat('00.0E+0',2222.22);&&& 22.2E+2&&& 明白了吗,全靠E右边的0来支配的。
本文已收录于以下专栏:
相关文章推荐
delphi的FormatFormat(Format,集合) Format(x=%d, [12]); //x=12 //最普通Format(x=%3d, [12]); //x= 12 //指定宽度Fo...
第一部分:
//指令类型 type
s := Format('最大整数是: %d; 最小整数是: %d',[MaxInt,Low(Inte...
【原创】json字符串的简易格式化(delphi实现)
由于工作需要,需要将json字符串进行格式化展示,方便手工调试。
在线的工具有很多,但一时没找到离线的库或方法。
因此自己写了个简...
Delphi Format 格式化数字
function Format(const Format: const Args: array of const): stri...
MSDN介绍:
/zh-cn/library/system.string.format.aspx
 C#格式化数值结果表
string.Format的五种重载
String.format()方法使用说明
在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。
1) 打开Excel
ExcelApplication1.C
procedure TForm1.btnSendClick(Sender: TObject);
idSMTP.Host := ''; //邮件发送服务器
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Delphi中Format的字符串格式化使用说明_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Delphi中Format的字符串格式化使用说明
阅读已结束,下载文档到电脑
想免费下载更多文档?
定制HR最喜欢的简历
下载文档到电脑,方便使用
还剩2页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
2、FormatDateTime的用法3、FormatFloat的用法——————————————一、Format函数的用法function Format(const Format: const Args: array of const): $[SysUtils.pas功能 返回按指定方式格式化一个数组常量的字符形式说明 这个函数是我在Delphi中用得最多的函数,现在就列举几个例子给你个直观的理解"%" [索引 ":"] ["-"] [宽度] ["." 摘要] 类型Format('x=%d', [12]); & & &//'x=12' //最普通Format('x=%3d', [12]); & & //'x= 12' //指定宽度Format('x=%f', [12.0]); & & //'x=12.00' //浮点数Format('x=%.3f', [12.0]); & &//'x=12.000' //指定小数Format('x=%8.2f'[12.0]) & & // 'x= & & 12.00' ;Format('x=%.*f', [5, 12.0]); //'x=12.00000' //动态配置Format('x=%.5d', [12]); & & //'x=00012' //前面补充0Format('x=%.5x', [12]); & & &//'x=0000C' //十六进制Format('x=%1:d%0:d', [12, 13]); //'x=1312' //使用索引Format('x=%p', [nil]); & & //'x=' //指针Format('x=%1.1e', [12.0]); //'x=1.2E+001' //科学记数法Format('x=%%', []); //'x=%' //得到"%"S := Format('%s%d', [S, I]); //S := S + IntToStr(I); //连接字符串Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用。&首先看它的声明:&function Format(const Format: const Args: array of const):&事实上Format方法有两个种形式,另外一种是三个参数的,主要区别在于它是线程安全的,&但并不多用,所以这里只对第一个介绍。&function Format(const Format: const Args: array of const):&Format参数是一个格式字符串,用于格式化Args里面的值的。Args又是什么呢,&它是一个变体数组,即它里面可以有多个参数,而且每个参数可以不同。&如以下例子:&Format(’my name is %6s’,[’wind’]);&返回后就是&my name is wind&现在来看Format参数的详细情况:&Format里面可以写普通的字符串,比如’my name is’&但有些格式指令字符具有特殊意义,比如"%6s"&格式指令具有以下的形式:&"%" [index ":"] ["-"] [width] ["." prec] type&它是以"%"开始,而以type结束,type表示一个具体的类型。中间是用来&格式化type类型的指令字符,是可选的。&先来看看type,type可以是以下字符:&d 十制数,表示一个整型值&u 和d一样是整型值,但它是无符号的,而如果它对应的值是负的,则返回时&是一个2的32次方减去这个绝对值的数如:Format(’this is %u’,[-2]);&返回的是:this is &f 对应浮点数&e 科学表示法,对应整型数和浮点数,&比如Format(’this is %e’,[-2.22]);&返回的是:this is -2.00E+000&等一下再说明如果将数的精度缩小&g 这个只能对应浮点型,且它会将值中多余的数去掉&比如Format(’this is %g’,[02.200]);&返回的是:this is 2.2&n 只能对应浮点型,将值转化为号码的形式。看一个例子就明白了&Format(’this is %n’,[]);&返回的是this is 4,552.22&注意有两点,一是只表示到小数后两位,等一下说怎么消除这种情况&二是,即使小数没有被截断,它也不会也像整数部分一样有逗号来分开的&m 钱币类型,但关于货币类型有更好的格式化方法,这里只是简单的格式化&另外它只对应于浮点值&Format(’this is %m’,[9552.21]);&返回:this is ¥9,552.21&//(* 转载敬请注明-本文出处:南山古桃(nsgtao)的百度空间:/nsgtao/ *)p 对应于指针类型,返回的值是指针的地址,以十六进制的形式来表示&例如:&var X:&p:^&begin&X:=99;&p:=@X;&Edit1.Text:=Format(’this is %p’,[p]);&&Edit1的内容是:this is &s 对应字符串类型,不用多说了吧&x 必须是一个整形值,以十六进制的形式返回&Edit1.Text:=Format(’this is %X’,[15]);&返回是:this is F&类型讲述完毕,下面介绍格式化Type的指令:&[index ":"] 这个要怎么表达呢,看一个例子&Format(’this is %d %d’,[12,13]);&其中第一个%d的索引是0,第二个%d是1,所以字符显示的时候&是这样 this is 12 13&而如果你这样定义:&Format(’this is %1:d %0:d’,[12,13]);&那么返回的字符串就变成了&this is 13 12&现在明白了吗,[index ":"] 中的index指示Args中参数显示的&顺序&还有一种情况,如果这样Format(’%d %d %d %0:d %d’, [1, 2, 3, 4])&将返回1 2 3 1 2。&如果你想返回的是1 2 3 1 4,必须这样定:&Format(’%d %d %d %0:d %3:d’, [1, 2, 3, 4])&但用的时候要注意,索引不能超出Args中的个数,不然会引起异常&如Format(’this is %2:d %0:d’,[12,13]);&由于Args中只有12 13 两个数,所以Index只能是0或1,这里为2就错了&[width] 指定将被格式化的值占的宽度,看一个例子就明白了&Format(’this is %4d’,[12]);&输出是:this is & 12&这个是比较容易,不过如果Width的值小于参数的长度,则没有效果。&如:Format(’this is %1d’,[12]);&输出是:this is 12&["-"] 这个指定参数向左齐,和[width]合在一起最可以看到效果:&Format(’this is %-4d,yes’,[12]);&输出是:this is 12 &,yes&["." prec] 指定精度,对于浮点数效果最佳:&Format(’this is %.2f’,[’1.1234]);&输出 this is 1.12&Format(’this is %.7f’,[’1.1234]);&输了 this is 1.1234000&而对于整型数,如果prec比如整型的位数小,则没有效果&反之比整形值的位数大,则会在整型值的前面以0补之&Format(’this is %.7d’,[1234]);&输出是:this is 0001234]&对于字符型,刚好和整型值相反,如果prec比字符串型的长度大&则没有效果,反之比字符串型的长度小,则会截断尾部的字符&Format(’this is %.2s’,[’1234’]);&输出是 this is 12&而上面说的这个例子:&Format(’this is %e’,[-2.22]);&返回的是:this is -2.00E+000&怎么去掉多余的0呢,这个就行啦&Format(’this is %.2e’,[-2.22]);&好了,第一个总算讲完了,应该对它的应用很熟悉了吧&///////////////////////////////////////////////////////////////&二、FormatDateTime的用法&它的声明为:&function FormatDateTime(const Format: DateTime: TDateTime):&&当然和Format一样还有一种,但这里只介绍常用的第一种&Format参数是一个格式化字符串。DateTime是时间类型。返回值是一种格式化后的&字符串&重点来看Format参数中的指令字符&c 以短时间格式显示时间,即全部是数字的表示&FormatDateTime(’c’,now);&输出为: 9:55:40&d 对应于时间中的日期,日期是一位则显示一位,两位则显示两位&FormatDateTime(’d’,now);&输出可能为1~31&dd 和d的意义一样,但它始终是以两位来显示的&FormatDateTime(’dd’,now);&输出可能为01~31&ddd 显示的是星期几&FormatDateTime(’ddd’,now);&输出为: 星期六&dddd 和ddd显示的是一样的。&但上面两个如果在其它国家可能不一样。&ddddd 以短时间格式显示年月日&FormatDateTime(’ddddd’,now);&输出为:&dddddd 以长时间格式显示年月日&FormatDateTime(’dddddd’,now);&输出为:日&e/ee/eee/eeee 以相应的位数显示年&FormatDateTime(’ee’,now);&输出为:04 (表示04年)&m/mm/mmm/mmmm 表示月&FormatDateTime(’m’,now);&输出为:8&FormatDateTime(’mm’,now);&输出为 08&FormatDateTime(’mmm’,now);&输出为 八月&FormatDateTime(’mmmm’,now);&输出为 八月&和ddd/dddd 一样,在其它国家可能不同&yy/yyyy 表示年&FormatDateTime(’yy’,now);&输出为 04&FormatDateTime(’yyyy’,now);&输出为 2004&h/hh,n/nn,s/ss,z/zzz 分别表示小时,分,秒,毫秒&t 以短时间格式显示时间&FormatDateTime(’t’,now);&输出为 10:17&tt 以长时间格式显示时间&FormatDateTime(’tt’,now);&输出为10:18:46&ampm 以长时间格式显示上午还是下午&FormatDateTime(’ttampm’,now);&输出为:10:22:57上午&//(* 转载敬请注明-本文出处:南山古桃(nsgtao)的百度空间:/nsgtao/ *)大概如此,如果要在Format中加普通的字符串,可以用双引号隔开那些&特定义的字符,这样普通字符串中如果含特殊的字符就不会被显示为&时间格式啦:&FormatDateTime(’"today is" c’,now);&输出为:today is
10:26:58&时间中也可以加"-"或"\"来分开日期:&FormatDateTime(’"today is" yy-mm-dd’,now);&FormatDateTime(’"today is" yy\mm\dd’,now);&输出为: today is 04-08-07&也可以用":"来分开时间&FormatDateTime(’"today is" hh:nn:ss’,now);&输出为:today is 10:32:23&/////////////////////////////////////////////////////////////////&三、FormatFloat的用法&常用的声明:&function FormatFloat(const Format: value: Extended):&和上面一样Format参数为格式化指令字符,value为Extended类型&为什么是这个类型,因为它是所有浮点值中表示范围最大的,如果传入该方法的参数&比如Double或者其它,则可以保存不会超出范围。&关键是看Format参数的用法&0 这个指定相应的位数的指令。&比如:FormatFloat(’000.000’,22.22);&输出的就是022.220&注意一点,如果整数部分的0的个数小于value参数中整数的位数,则没有效果&如:FormatFloat(’0.00’,22.22);&输出的是:22.22&但如果小数部分的0小于value中小数的位数,则会截去相应的小数和位数&如:FormatFloat(’0.0’,22.22);&输出的是:22.2&也可以在整数0中指定逗号,这个整数位数必须大于3个,才会有逗号出现&FormatFloat(’0,000.0’,2222.22);&输出是:2,222.2&如果这样FormatFloat(’000,0.0’,2222.22);&它的输出还是:2,222.2&注意它的规律&# 和0的用法一样,目前我还没有测出有什么不同。&FormatFloat(’##.##’,22.22);&输出是:22.00&E 科学表示法,看几个例子大概就明白了&FormatFloat(’0.00E+00’,2222.22);&输出是 2.22E+03&FormatFloat(’’,2222.22);&输出是 &FormatFloat(’00.0E+0’,2222.22);&22.2E+2来自:http://wmingyan./blog/static//
阅读(192)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_',
blogTitle:'【转】Delphi、C++ Builder之Format函数详细的用法',
blogAbstract:'1、Format函数的用法2、FormatDateTime的用法3、FormatFloat的用法——————————————一、Format函数的用法function Format(const Format: const Args: array of const): $[SysUtils.pas功能 返回按指定方式格式化一个数组常量的字符形式说明 这个函数是我在Delphi中用得最多的函数,现在就列举几个例子给你个直观的理解\"%\" [索引 \":\"] [\"-\"] [宽度] [\".\" 摘要] 类型',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:4,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'暂无介绍',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 win7怎样格式化硬盘 的文章

 

随机推荐