我写的,参考一下 function rRound(r:TNumber;Dec:integer):TNumber; var i:integer; temp:TNumber; begin temp:=1; if Dec>=0 then for i:=1 to Dec do temp:=temp*10 else for i:=-1 downto Dec do temp:=temp/10; result:=round(r*temp)/temp; end;
Trunc 不是四舍五入,而是把小数点后面的数字截去。
delphi 没有自带四舍五入函数!看下面。function FourOutFiveIn(xValue:Double;xpos:integer):Double; begin if xValue < 0 then Result := DsTrunc( xValue * exp( xPos * ln(10)) - 0.5) / exp( xPos * ln(10)) else Result := DsTrunc( xValue * exp( xPos * ln(10)) + 0.5) / exp( xPos * ln(10)); if Abs( Result ) < SeedValue then Result := 0; end;
和Round
你试试!
我想你要的是转化为保留N位上数的字符串吧,
如果是这样,请用:format('%.nf',[dValue]);
function rRound(r:TNumber;Dec:integer):TNumber;
var
i:integer;
temp:TNumber;
begin
temp:=1;
if Dec>=0 then
for i:=1 to Dec do temp:=temp*10
else
for i:=-1 downto Dec do temp:=temp/10;
result:=round(r*temp)/temp;
end;
不是四舍五入,而是把小数点后面的数字截去。
begin
if xValue < 0 then
Result := DsTrunc( xValue * exp( xPos * ln(10)) - 0.5) / exp( xPos * ln(10))
else
Result := DsTrunc( xValue * exp( xPos * ln(10)) + 0.5) / exp( xPos * ln(10));
if Abs( Result ) < SeedValue then Result := 0;
end;
还是你小子聪明,拿分吧
你不把它也写出来,这个函数有什么用?
format('%.2f',[123.456]);