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

标签 delphi delphi-xe3 currency-formatting

我有一个类似的案例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/

相关文章:

android - 使用delphi使用外部图库打开图像

delphi - Delphi 7 和 Delphi 2007 中接口(interface)的区别

delphi - catch 启动应用程序

Delphi重新创建 Canvas

德尔福 F2084 内部错误 : AV07953449-R26D7474C-0

delphi - 如何检测没有焦点的控件中的修改键更改?

java - 接受不带小数点的货币格式的 JFormattedTextField

ios - Swift:使用本地化的百万和十亿文本格式化货币

xml - 如何在Delphi中读取XML文件?

PHP - setlocale(LC_MONETARY, 'en_IN' );不工作