gpt4 book ai didi

delphi - 是否有类似StrToCurr的函数可以处理数千个分隔符?

转载 作者:行者123 更新时间:2023-12-03 18:57:04 25 4
gpt4 key购买 nike

我有一个类似的案例as in this question

procedure TForm2.FormCreate(Sender: TObject);
var
S: string;
C: Currency;
FormatSettings: TFormatSettings;
begin
S := '1.000.000,00';
FormatSettings := TFormatSettings.Create;
FormatSettings.ThousandSeparator := '.';
FormatSettings.DecimalSeparator := ',';
// raises an Exception which is "as designed" as per the documentation
C := StrToCurr(S, FormatSettings);
ShowMessage(FormatCurr(',0.00', C));
end;


引用大卫:


因此,传递包含千位分隔符的字符串是错误的
此功能。


那么,Delphi是否有任何内置函数可以解析包含千位分隔符的货币字符串?

最佳答案

该设计(缺陷)的解决方案很简单:定义自己的功能。

unit MyFixForSysUtils;

interface

function StrToCurr(const Str: string): Currency; overload;
function StrToCurr(Str: string; const FormatSettings : TFormatSettings): Currency; overload;

implementation

uses SysUtils;

function StrToCurr(Str: string; const FormatSettings : TFormatSettings): Currency;
begin
Str:= StringReplace(Str, FormatSettings.ThousandSeparator, '', [rfReplaceAll]);
Result:= SysUtils.StrToCurr(Str, FormatSettings);
end;

function StrToCurr(const Str: string): Currency;
begin
Result:= StrToCurr(Str, FormatSettings);
end;


如果您确定自己的版本在范围上比 sysutils更近,则无需更改代码:

uses
SysUtils,
MyFixForSysUtils, <-- contains the above function
.... other units.


现在,Delphi将选择固定功能而不是损坏的功能。

有关此概念的更多信息,请参见: Delphi interposer

关于delphi - 是否有类似StrToCurr的函数可以处理数千个分隔符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38817372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com