DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: shz0000
今日帖子: 0
在线用户: 9
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 yiqiu (忆秋) ★☆☆☆☆ -
普通会员
2003/9/17 10:37:37
标题:
有点不懂 浏览:2035
加入我的收藏
楼主:   我看了一个四舍五入的FORMAT方法但是有点看不懂。请大虾给我看一下。谢谢]
  format('%*.*f'[1,2,345345.435])
  这返回的数值是345345.44 对于 其中的数字2 我知道是保留小数点的位数。 但其中的1我不明白。还偶前面的% 和F 那是什么意思啊? 我是一名新手。谢谢。!!
----------------------------------------------
-
作者:
男 skertone (奇奇怪) ★☆☆☆☆ -
盒子活跃会员
2003/9/17 11:20:43
1楼: Delphi的帮助也还可以凑合着用呀

Format strings passed to the string formatting routines contain two types of objects -- literal characters and format specifiers. Literal characters are copied verbatim to the resulting string. Format specifiers fetch arguments from the argument list and apply formatting to them.
Format specifiers have the following form:

"%" [index ":"] ["-"] [width] ["." prec] type

A format specifier begins with a % character. After the % come the following, in this order:

An optional argument index specifier, [index ":"]
  An optional left justification indicator, ["-"]
  An optional width specifier, [width]
  An optional precision specifier, ["." prec]
  The conversion type character, type

The following table summarizes the possible values for type:

d  Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros.
u  Unsigned decimal. Similar to 'd' but no sign is output.
e  Scientific. The argument must be a floating-point value. The value is converted to a string of the form "-d.ddd...E+ddd". The resulting string starts with a minus sign if the number is negative. One digit always precedes the decimal point.The total number of digits in the resulting string (including the one before the decimal point) is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present. The "E" exponent character in the resulting string is always followed by a plus or minus sign and at least three digits.
f  Fixed. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative.The number of digits after the decimal point is given by the precision specifier in the format string梐 default of 2 decimal digits is assumed if no precision specifier is present.
g  General. The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string梐 default precision of 15 is assumed if no precision specifier is present.Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format.
n  Number. The argument must be a floating-point value. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The "n" format corresponds to the "f" format, except that the resulting string contains thousand separators.
m  Money. The argument must be a floating-point value. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator, and CurrencyDecimals global variables, all of which are initialized from the Currency Format in the International section of the Windows Control Panel. If the format string contains a precision specifier, it overrides the value given by the CurrencyDecimals global variable.
p  Pointer. The argument must be a pointer value. The value is converted to an 8 character string that represents the pointers value in hexadecimal.
s  String. The argument must be a character, a string, or a PChar value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated.
x  Hexadecimal. The argument must be an integer value. The value is converted to a string of hexadecimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has fewer digits, the resulting string is left-padded with zeros.
Conversion characters may be specified in uppercase as well as in lowercase梑oth produce the same results.
For all floating-point formats, the actual characters used as decimal and thousand separators are obtained from the DecimalSeparator and ThousandSeparator global variables.
Index, width, and precision specifiers can be specified directly using decimal digit string (for example "%10d"), or indirectly using an asterisk character (for example "%*.*f"). When using an asterisk, the next argument in the argument list (which must be an integer value) becomes the value that is actually used. For example, 

Format('%*.*f', [8, 2, 123.456]) 

is the same as 

Format('%8.2f', [123.456]).


A width specifier sets the minimum field width for a conversion. If the resulting string is shorter than the minimum field width, it is padded with blanks to increase the field width. The default is to right-justify the result by adding blanks in front of the value, but if the format specifier contains a left-justification indicator (a "-" character preceding the width specifier), the result is left-justified by adding blanks after the value.

An index specifier sets the current argument list index to the specified value. The index of the first argument in the argument list is 0. Using index specifiers, it is possible to format the same argument multiple times. For example "Format('%d %d %0:d %1:d', [10, 20])" produces the string '10 20 10 20'.

The format strings are used by the following routines:...........
----------------------------------------------
按此在新窗口浏览图片 按此在新窗口浏览图片 按此在新窗口浏览图片
作者:
男 yiqiu (忆秋) ★☆☆☆☆ -
普通会员
2003/9/17 11:29:07
2楼: 能不能简练一点说明问题啊。 CHINASE  THANKYOU
----------------------------------------------
-
作者:
男 sunwards (SUNWARDS) ★☆☆☆☆ -
普通会员
2003/9/17 21:43:27
3楼: 不会的话直接用ROUND就是,注意一点的是0。5的处理,DELPHI中是4舍6入的,如果你要4舍5入就判断到0。5时强行加0.1就可以了。如果是对第几位小数做,不用说了吧!round(x*10)/10就OK!
----------------------------------------------
1
作者:
男 linfenypp (ypp) ★☆☆☆☆ -
盒子活跃会员
2003/9/18 13:30:31
4楼: "1"是格式化后字符串的长度,超过实际长度,左边以空格填满,小于实际长度,则按实际长度显示;
"%"是格式化字符串开始的标志,就像c语言里的printf
"f"是数据类型,这里指fixed类型.

顺便说一下:
  你的函数引用格式错了,应该是:format('%*.*f',[1,2,345345.435])
----------------------------------------------
-
作者:
男 linfenypp (ypp) ★☆☆☆☆ -
盒子活跃会员
2003/9/18 13:31:03
4楼: "1"是格式化后字符串的长度,超过实际长度,左边以空格填满,小于实际长度,则按实际长度显示;
"%"是格式化字符串开始的标志,就像c语言里的printf
"f"是数据类型,这里指fixed类型.

顺便说一下:
  你的函数引用格式错了,应该是:format('%*.*f',[1,2,345345.435])
----------------------------------------------
-
作者:
男 yiqiu (忆秋) ★☆☆☆☆ -
普通会员
2003/9/21 9:11:10
5楼: to :linfenypp
   我感觉有点问题。就是你所说的。如果1是实际长度的话那现在函数的返回值是55.44 对吗? 可是现在结果返回为345345.44不知道为什么。
----------------------------------------------
-
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行50.96436毫秒 RSS