要使小数点左边固定某长度(如5位),小数点右边保留2位,要使用哪个格式化函数?
如:
2.1
3333.2格式化后变成:
____2.10
_3333.20再次,暂用“_”表示空格符
如:
2.1
3333.2格式化后变成:
____2.10
_3333.20再次,暂用“_”表示空格符
解决方案 »
- 运行的时候提示project XX.exe raised exception class EConvertError with message "is not a valid integer value"是怎么回事?
- 下载了一源,打开后弹出Class TWebBrowser not found ,是什么问题啊,
- 请各位大侠帮忙看看小虾的程序那儿错了!(图片查询显示问题)
- 如何用PostMessage发个消息使窗体中一个按钮变为可用或者不可用?
- 怎么样禁止输入重复记录呢?
- 一个特别简单的问题,关于将access数据库数据导出为文本格式,在线等待。帮忙就给分。
- 如何取消MDI主窗口背景图片,让MDI主窗口显示指定颜色呢
- 如何调用MapInfo地图
- 跨语言的Web Service调用的问题!
- 进销存中的模糊查找
- 如何写这样的查询?
- 类的成员会自动创建吗?
s1:string;
...
formatfloat('0.00',strtofloat(s1))
uses SysUtils
或者uses Math,用roundto
但是,循环执行下面语句后是左对齐的,不知道怎么格式化,拜托问问大家,谢谢!!!
Printer.Canvas.TextOut(x+3071,y,'┃'+FormatFloat('0.00',ADOQuery1.FieldByName('总金额').AsFloat));
begin
if 能找到'.' then
begin
sub1 : = .之前;
sub2 := .之后;
Result := LeadingSpace(sub1,5) + '.' +AddZero(sub2,2);
end
else
begin
Result := LeadingSpace(你的串,5) + '.00'
end;
end;
function LeadingSpace(.之前串,位数):string;
begin
while Length(Result)<位数 do Result:=' '+Result;
end;
function AddZero(.之后串,位数):string;
begin
while Length(Result)<位数 do Result:=Result+'0';
end;
value是你的值,必须为数值型,一般用Real型。
unit my_func;interfacefunction add_space(from_str:string):string;implementationfunction add_space(from_str:string):string;
var
TempStr:String;
i:integer;
begin
TempStr:=from_str; //from_str为需要格式的字符串
if length(TempStr)<8 then //假设定长为8(小数点之前5为,小数点之后2位)
begin
for i:=1 to 8-Length(TempStr) do
begin
TempStr:=' '+TempStr;
end;
end;
from_str:=TempStr;
end;end.
这样调用:
add_space(FormatFloat('0.00',12));跟踪函数执行过程,在循环里运行都是正常的(能在字符串加上前头补上前导空格),但是返回值却是一个空格,哪里出错了?
function FormatFloatS(Value:Double;
HeadLen,//头长度
EndLen:integer//尾长度
):String;
var
m,heads,ends:String;
i,k:integer;
begin
m:=FormatFloat('#.#',Value);
i:=pos('.',m);
if i>0 then
begin
heads:=Copy(m,1,i-1);
ends:=Copy(m,i+1,length(m)-i);
if length(heads)<= HeadLen then
begin
while length(heads)<headLen do
begin
heads:='0'+heads;
end;
end
else
begin
heads:=Copy(heads, HeadLen-5,HeadLen);
end;
if length(ends)<= EndLen then
begin
while length(ends)< EndLen do
begin
ends:= ends+'0';
end;
end
else
begin
heads:=Copy(heads,1,HeadLen);
end;
end
else
begin
heads:=m;
if length(heads)<= HeadLen then
begin
while length(heads)<headLen do
begin
heads:='0'+heads;
end;
end
else
begin
heads:=Copy(heads, HeadLen-5,HeadLen);
end;
ends:='';
while length(ends)< EndLen do
begin
ends:= ends+'0';
end;
end;
Result:=heads+'.'+ends;
end;