如何将123.235处理后为123.24(100分)

  • 主题发起人 主题发起人 yanghx_yhx
  • 开始时间 开始时间
Y

yanghx_yhx

Unregistered / Unconfirmed
GUEST, unregistred user!
使用什么函数可以实现?
小数点后四舍五入保留两位
谢谢!!!
 
1、formatfloat('0.00',123.235)
2、round(123.235*100)/100
3、select round(123.235,2) from table where 1<>1
 
formatfloat('0.00',123.235)
 
// 四舍五入
function TForm1.SSWR(Values: Double; K: Integer): Double;
var
S: String;
EF: Extended;
begin
S := '#.'+StringOfChar('0',K);
EF := StrToFloat(FloatToStr(Values)); //防止浮点运算的误差
Result := StrToFloat(FormatFloat(S,EF));
end;
 
round(123.235,2)
 
自己编写:
interge i=i+0.005
 
function RoundTo(const AValue: Double; const ADigit: TRoundToRange): Double;

Description

Call RoundTo to round AValue to a specified power of ten.

AValue is the value to round.

ADigit indicates the power of ten to which you want AValue rounded. It can be any value from ?7 to 37 (inclusive).

RoundTo uses 揃anker抯 Rounding?to determine how to round values that are exactly midway between the two values that have the desired number of significant digits. This method rounds to an even number in the case that AValue is not nearer to either value.

The following examples illustrate the use of RoundTo:

Expression Value

RoundTo(1234567, 3) 1234000
RoundTo(1.234, -2) 1.23
RoundTo(1.235, -2) 1.24
RoundTo(1.245, -2) 1.24
 
谢谢!
其实最好用的是formatfloat('0.00',12.3456)
 
就是,我也觉得用formatfloat比较好
 
Delphi的四舍五入有问题
 
还是用 :formatfloat 吧,
看这就是RoundTo的毛病:RoundTo(1.245, -2) 1.24 。
 
Function Test(const f1:String):String;
begin
Result:= Copy(Trim(FloatToStr((StrToFloat(f1)+0.005))),1,6)
end;
//调用:ShowMessage(Test('123.235'));
//输出:123.235经过处理就输出了123.24--------符合题意!

 
后退
顶部