SetRoundMode() <----可以设置四舍五入的模式--------Sets the FPU rounding mode.UnitMathCategoryFPU controltype TFPURoundingMode = (rmNearest, rmDown, rmUp, rmTruncate); function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;DescriptionCall SetRoundingMode to specify how the FPU handles rounding issues. The rounding mode can be any of the following values:Value MeaningrmNearest Rounds to the closest value. rmDown Rounds toward negative infinity. rmUp Rounds toward positive infinity. rmTruncate Truncates the value, rounding positive numbers down and negative numbers up.
b:=2.96445*100-a=0.445.
然后通过判断b是否大于0。5来进行四舍五入。这样准确,不会出现ZyxIp(绝望中...) 的问题。
function SetRoundMode(const RoundMode: TFPURoundingMode): TFPURoundingMode;DescriptionCall SetRoundingMode to specify how the FPU handles rounding issues. The rounding mode can be any of the following values:Value MeaningrmNearest Rounds to the closest value.
rmDown Rounds toward negative infinity.
rmUp Rounds toward positive infinity.
rmTruncate Truncates the value, rounding positive numbers down and negative numbers up.
showmessage( floattostr( Ceil(1*2.964446*100) / 100 ) );
formatfloat('#.##',2.964446);
忘了要不要带引号了,你自己试吧。
mynum:Double;
mynum:=StrToFloat(FormatFloat('##.##',2.964446));
对于取小数点多少位之后四舍五入,这样:
Trunc(x*a+0.5)/a,这里a为1,10,100等10的n-1次方(n=1,2,3,...)。
所以,你要取0.54663小数点后面3位并四舍五入,就这样:
0.54663*100+0.5=546.63+0.5=547.13,Trunc之后时547,然后/100就成为0.547了.--------
这个是CSDN上的摘录的。是哪位网友发的帖子我忘记了,但还是要感谢他的代码。
使用下面语句怎么就是得不到2.97数据的呢?
showmessage(floattostr(round(1*2.964446*100)/100));
如何将2.964446使用四舍五入的方法得到2.97呢?************
四舍五入怎么能得到2.97呢?四舍五入的规则都忘了吧.楼主?
2.964->2.96
以最后一位数字为基准!
FloatToStrF(); 函数 总体思路:
ShowMessage(FloatToStrF(2.964446,ffFixed,15,5);//=2.96445 (先取小数点后五位)
ShowMessage(FloatToStrF(2.96445,ffFixed,15,4);//=2.9645(再取小数点后四位)
ShowMessage(FloatToStrF(2.9645,ffFixed,15,3);//=2.9644(再取小数点后三位)
ShowMessage(FloatToStrF(2.965,ffFixed,15,2);//=2.97(再取小数点后二位)
1、先找到小数点位置。
2、倒着一个个的取数。
//
procedure TForm1.Button1Click(Sender: TObject);
var
d :double;
i :integer;
j :integer;
s :string;
begin
d := 2.964446;
s :=FloatToStr(d);
i:=0;
j :=0;
i := pos('.',s);//找到小数点的位置.
memo1.Lines.Clear;
i := Length(s)-i;//取出小数据点后面有多少位小数.
for j:=i-1 downto 2 do //到2是保留二位小数点.倒着取数.
begin
s := FloatToStrF(d,ffFixed,15,j); //Delphi 有问题,当J=4时,d=2.96445 结果应该 是 2.9645才对呀,但是在这个循还体中却为 2.9644 郁闷呀!
//要是不信你可以试试 ShowMessage(FloatToStrF(2.96445,ffFixed,15,4));//=2.9645
memo1.Lines.Add('s:='+s+' j:'+intToStr(j));
d := StrToFloat(s);
memo1.Lines.Add('d:= '+FloatTostr(d));
end;
caption :=s;
end;
//请高人指点迷津
ShowMessage(FloatToStrF(2.96445,ffFixed,15,4));//=2.9645 这样就是对的。
function RoundEx(const val: string; ipos: integer = 2): string; overload;
function RoundEx(const val: double; ipos: integer = 2): double; overload;
{-----------------------------------------------------------------------------
Procedure: RoundEx
Author: Administrator
Date: 18-八月-2003
Arguments: const val: double; ipos: integer = 2
Result: double 取四舍五入的函数。
-----------------------------------------------------------------------------}function RoundEx(const val: double; ipos: integer = 2): double;
var
dTemp: Extended;
i, j: integer;
str: string;
begin
result := val;
dTemp := val;
str := FloatToStr(dtemp);
i := 0;
j := 0;
i := pos('.', str);
i := Length(str) - i;
if i <= ipos then //如果小数位数小于要取的位数,就直接在后面补0。
begin
str := FloatToStrF(dTemp, ffFixed, 15, ipos);
result := StrToFloat(str);
exit;
end;
for J := i - 1 downto ipos do
begin
str := FloatToStrF(dtemp, ffFixed, 15, j);
dtemp := StrToFloat(str);
end;
result := dtemp;
end;function RoundEx(const val: string; ipos: integer = 2): string;
var
dTemp: Extended;
i, j: integer;
str: string;
fmt: TFormatSettings;
begin
result := val;
dTemp := strToFloat(val);
str := FloatToStr(dtemp);
i := 0;
j := 0;
i := pos('.', str);
i := Length(str) - i;
if i <= ipos then //如果小数位数小于要取的位数,就直接在后面补0。
begin
str := FloatToStrF(dTemp, ffFixed, 15, ipos);
result := str;
exit;
end;
for J := i - 1 downto ipos do
begin
str := FloatToStrF(dtemp, ffFixed, 15, j);
dtemp := strtoFloat(str);
end;
result := str;
end;