Delphi中如何保留小数点后2位?

发布于 2024-11-01 01:22:47 字数 207 浏览 7 评论 0原文

我从数据库表中选择了列,并且希望该数据仅包含两位小数。我有:

SQL.Strings = ('select '#9'my_index '#9'his_index,'...
  • #9 是什么?
  • 如何处理我选择的数据,使其只保留两位小数?

我对德尔福很陌生。

I have selected columns from a database table and want this data with two decimal places only. I have:

SQL.Strings = ('select '#9'my_index '#9'his_index,'...
  • What is that #9?
  • How can I deal with the data I selected to make it only keep two decimal places?

I am very new to Delphi.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

2024-11-08 01:22:48
function Round2(aValue:double):double;    
begin    
  Round2:=Round(aValue*100)/100;    
end;
function Round2(aValue:double):double;    
begin    
  Round2:=Round(aValue*100)/100;    
end;
入怼 2024-11-08 01:22:48

#9 是制表符。

如果 f 是浮点变量,您可以执行 FormatFloat('#.##', f) 来获取 f 的字符串表示形式> 不超过 2 位小数。

#9 is the tab character.

If f is a floating-point variable, you can do FormatFloat('#.##', f) to obtain a string representation of f with no more than 2 decimals.

顾挽 2024-11-08 01:22:48

对于 Float 到 Float(例如,小数点后两位)四舍五入检查 this< /a> 来自文档。也给出了足够的例子。它使用银行家四舍五入。

x := RoundTo(1.235, -2); //gives 1.24

请注意,简单截断到小数点后两位(如 Format() 中)、四舍五入到整数和四舍五入到浮点数之间存在差异。

For Float to Float (with 2 decimal places, say) rounding check this from documentation. Gives sufficient examples too. It uses banker's rounding.

x := RoundTo(1.235, -2); //gives 1.24

Note that there is a difference between simply truncating to two decimal places (like in Format()), rounding to integer, and rounding to float.

奢望 2024-11-08 01:22:48

如今,SysUtils 单元包含解决方案:

System.SysUtils.FloatToStrF( singleValue, 7, ffFixed, 2 );
System.SysUtils.FloatToStrF( doubleValue, 15, ffFixed, 2 );

如果所需的小数点/千位分隔符与当前系统区域设置不同,您可以传递 +1 TFormatSettings 参数。

Nowadays the SysUtils unit contains the solution:

System.SysUtils.FloatToStrF( singleValue, 7, ffFixed, 2 );
System.SysUtils.FloatToStrF( doubleValue, 15, ffFixed, 2 );

You can pass +1 TFormatSettings parameter if the requiered decimal/thousand separator differ from the current system locale settings.

总攻大人 2024-11-08 01:22:48

内部浮点格式例程仅适用于简单数字 > 1

您需要为通用小数位限制器做一些更复杂的事情,使其在定点和值 << 上都能正常工作。 1 用科学记数法表示。

我使用这个例程

function TForm1.Flt2str(Avalue:double; ADigits:integer):string;
var v:double; p:integer; e:string;
begin
  if abs(Avalue)<1 then
  begin
    result:=floatTostr(Avalue);
    p:=pos('E',result);
    if p>0 then
    begin
      e:=copy(result,p,length(result));
      setlength(result,p-1);
      v:=RoundTo(StrToFloat(result),-Adigits);
      result:=FloatToStr(v)+e;
    end else
      result:=FloatToStr(RoundTo(Avalue,-Adigits));
  end
  else
    result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;

所以,当数字=2时,1.2349舍入为1.23,1.2349E-17舍入为1.23E-17

The internal float format routines only work with simple numbers > 1

You need to do something more complicated for a general purpose decimal place limiter that works correctly on both fixed point and values < 1 with scientific notation.

I use this routine

function TForm1.Flt2str(Avalue:double; ADigits:integer):string;
var v:double; p:integer; e:string;
begin
  if abs(Avalue)<1 then
  begin
    result:=floatTostr(Avalue);
    p:=pos('E',result);
    if p>0 then
    begin
      e:=copy(result,p,length(result));
      setlength(result,p-1);
      v:=RoundTo(StrToFloat(result),-Adigits);
      result:=FloatToStr(v)+e;
    end else
      result:=FloatToStr(RoundTo(Avalue,-Adigits));
  end
  else
    result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;

So, with digits=2, 1.2349 rounds to 1.23 and 1.2349E-17 rounds to 1.23E-17

霊感 2024-11-08 01:22:48

这对我有用:

Function RoundingUserDefineDecaimalPart(FloatNum: Double; NoOfDecPart: integer): Double;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber := FloatToStr(FloatNum);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result := StrToFloat
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, NoOfDecPart))
     Else
          Result := FloatNum;
End;

This worked for me :

Function RoundingUserDefineDecaimalPart(FloatNum: Double; NoOfDecPart: integer): Double;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber := FloatToStr(FloatNum);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result := StrToFloat
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, NoOfDecPart))
     Else
          Result := FloatNum;
End;
负佳期 2024-11-08 01:22:48
Function RealFormat(FloatNum: Double): string;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber:=StringReplace(FloatToStr(FloatNum),',','.',[rfReplaceAll]);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result :=
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, 2))
     Else
          Result := FloatToStr(FloatNum);
End;
Function RealFormat(FloatNum: Double): string;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber:=StringReplace(FloatToStr(FloatNum),',','.',[rfReplaceAll]);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result :=
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, 2))
     Else
          Result := FloatToStr(FloatNum);
End;
还不是爱你 2024-11-08 01:22:48

对于分隔符后面的 N 个地方使用

function round_n(f:double; n:nativeint):double;
var i,m : nativeint;
begin
  m := 10;
  for i := 1 to pred(n) do
    m := m * 10;

  f := f * m;
  f := round(f);
  result := f / m;      
end;

For N Places behind the seperator use

function round_n(f:double; n:nativeint):double;
var i,m : nativeint;
begin
  m := 10;
  for i := 1 to pred(n) do
    m := m * 10;

  f := f * m;
  f := round(f);
  result := f / m;      
end;
不必你懂 2024-11-08 01:22:47

#9 是代码为 9、TAB 的字符。

如果您想将浮点值转换为具有 2 位小数的字符串,您可以使用格式化函数之一,例如 Format()

var
  d: Double;
  s: string;
...
d := Sqrt(2.0);
s := Format('%.2f', [d]);

#9 is the character with code 9, TAB.

If you want to convert a floating point value to a string with 2 decimal places you use one of the formatting functions, e.g. Format():

var
  d: Double;
  s: string;
...
d := Sqrt(2.0);
s := Format('%.2f', [d]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文