如何将数字转换成特定的字符串?(50分)

W

wolf_zj

Unregistered / Unconfirmed
GUEST, unregistred user!
要将数字转换成字符串类型,字符串要求长度六位,不足六位的前面用零补满,
不知该如何转换?
 
var
xx:string;
yy:integer;
begin
yy:=1235;
xx:=inttostr(yy);
xx:=stringofchar('0',6-length(xx))+xx;
showmessage(xx);

end;
yy大于6位不用保护
 
fomatfloat('000000',f)
 
formatfloat('要转成的字符串',数值);

[:)][:)][:)][:)]
 
张剑波说的挺好,当然做个循环也可以啊。
 
function Num2Str(X: Integer): String;
begin
if x < 100000 then begin
Result:= IntToStr(X+100000);
Result[1]:= 0;
end else Result:= IntToStr(X);
end;
 
呵呵!用Format也可以,试试这个

write(Format('%.6D',[345]));
 
Format省事,看Delphi的帮助
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.
 
to: twos
还有帮助如下
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.
~~~~~~

其实Delphi中的Format做不到这点,不如C的sprintf好用。

ugvanxk的FormatFloat('000000',n) 是正确有效地

还有一种好玩的实现方法
s: string
s := IntToHex(StrToInt('$'+IntToStr(n)),6);
 
to SS2000:
Format怎么可能做不到.
你自己试试这个:ShowMessage(Format('%.6D',[345]));
看看前面是加的空格,还是0。
其它的比如FormatFloat等等,都是用FormatStr变来的;而Format,却是约等于
FormatStr,更强大,更灵活。
 
to:BeginDelphi
呵呵,你说的确实可以,我以前总是用Format('%06D',[345]),忘了加小数点了
看来是我没有用好,发表错误结论了。不过为了这个我还自己编了函数来实现,
真是老土了,我这就去把我编的函数给废了,还是用Format函数。
 
呵呵,又学了一招
 
顶部