读书人

FormatFloat,该如何处理

发布时间: 2012-06-01 16:46:36 作者: rapoo

FormatFloat
FormatFloat('0.#', 9.99);

我想让这代码返回9.9,而不是10
怎么处理?

[解决办法]
将9.99转为string,然后copy,或leftstr
[解决办法]
FormatFloat是4舍5入的,你要1位小数,后面的却丢掉;
显示FormatFloat,Format,Round...等等这些函数都不能用。
自己转换成字符串截取吧

[解决办法]
用Trunc来取不要用Formatfloat,那样会自动四舍五入的。
Edit1.text=‘9.99’;
FloatToStr(Trunc(StrToFloat(edit1.Text)*10)/10);
[解决办法]
更新一下

Delphi(Pascal) code
  function KeepDigitsAfterDecimalPoint(v: Extended; c: Byte): string;  var    i: Integer;  begin    Result := FloatToStr(v);    i := Pos('.', Result);    if i = 0 then    begin      Result := result + '.';      i := Length(Result)    end;    if c = 0 then      Delete(Result, i, Maxint)    else      if Length(Result) - i >= c then        Result := Copy(Result, 1, i+c)      else        Result := Result + DupeString('0', c-(Length(Result) - i))  end; 

读书人网 >.NET

热点推荐