delphi “div”、“mod”、“”除法运算符的区别与使用方法(附带FORMAT使用方法)

Delphi中和除法相关的算术运算符有:

div、mod和符号“”

下面分别对他们的作用、操作数类型和返回值类型进行一下介绍:


div:对2个整数进行除,取商,操作数需是integer类型,值也是integer。


:对2个数进行除,取商,操作数可以为integer和real,值为real类型。

mod:取2个数相除的余数,操作数均为integer类型,值为integer。

所以使用与div操作时要注意的结果应为实型(Real);

Format使用方法例子:

  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'//前面补充0

  Format('x=%.5x',[12]);//'x=0000C'//十六进制

  Format('x=%1:d%0:d',[12,13]);//'x=1312'//使用索引

  Format('x=%p',[nil]);//'x=00000000'//指针(8bit)

  Format('x=%1.1e',[12.0]);//'x=1.2E+001'//科学记数法

  Format('x=%%',[]);//'x=%'//得到"%"

  S:=Format('%s%d',[S,I]);//S:=S+StrToInt(I);//连接字符串 

原文地址:https://www.cnblogs.com/Master-Qi/p/9767532.html