【发布时间】:2015-11-17 18:18:35
【问题描述】:
在我的 Delphi XE2 项目中,我使用一些实变量来计算一些与凭证相关的数据。我写了以下代码:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Math;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ServiceTax, RetailPrice, ProcessingFee, VoucherValue, AccountBalance, Airtimepercentage : real;
begin
RetailPrice := StrToFloatDef(Edit1.text, 0);
ServiceTax := StrToFloatDef(Edit2.text, 0);
if (RetailPrice*(10/100) <= 5) then ProcessingFee := RetailPrice*(10/100) else ProcessingFee := 5;
VoucherValue := (RetailPrice/(1+(ServiceTax/100)) - ProcessingFee);
AccountBalance := StrToFloatDef(Edit5.text, 0);
AirTimePercentage := (AccountBalance/VoucherValue)*100;
Edit3.Text := FloatToStrF(ProcessingFee, ffFixed, 16, 6);
Edit4.Text := FloatToStrF(VoucherValue, ffFixed, 16, 6);
Edit6.Text := FloatToStrF(AirTimePercentage, ffFixed, 16, 6);
end;
end.
但问题是VoucherValue 是一个浮点数。它包含一个很长的小数点,但我的要求是最多只有两个小数点,或者可能是一个长小数点,但在两个小数点之后(例如 12.19),所有数字都为零(例如 12.190000)。所以我尝试了FormatFloat如下:
VoucherValue := StrToFloatDef(FormatFloat('0.##', FloatToStrF((RetailPrice/(1+(ServiceTax/100)) - ProcessingFee), ffFixed, 16, 6)), 0);
但我无法编译并得到如下错误:
[dcc32 Error] Unit1.pas(46): E2250 There is no overloaded version of 'FormatFloat' that can be called with these arguments
FormatFloat 的另一个缺点是它可以截断(即 12.129999 到 12.12)但不能近似(即 12.129999 到 12.13),但我需要近似值。
另一种解决方案是使用另一个字符串变量,但我不喜欢使用。
请给我建议。
【问题讨论】:
-
另一天,另一个需要阅读本文的人:WECSSKAFP (Goldberg,1991) docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
-
你不能使用浮点类型来赚钱。改为使用货币。
标签: delphi delphi-xe2