floating point - Make VarToDoubleAsString use Delphi settings (not OS settings) -


when 1 assigns variant containing string value floating point variable delphi calls vartodoubleasstring conversion, in turn uses os settings decimal , thousand separator (via varr8fromstr). problematic if 1 has change sysutils.decimalseparator , sysutils.thousandseparator. example run following program:

program varstrtofloat;  {$apptype console}  uses   sysutils,   math;  function formatfloatusingdelphisettings(value: extended): string; begin   result := formatfloat('#,##0.00', value); end;  procedure test(const amsg: string); var   r1, r2: extended;   s1, s2: string;   v: variant; begin   r1 := 5432.1;   s1 := formatfloatusingdelphisettings(r1);   v := s1; // <== conversion uses os settings   r2 := v;   s2 := formatfloatusingdelphisettings(r2);    write(amsg: 8, s1: 10, s2: 10, '  ');   if samevalue(r1, r2)     writeln('ok')   else     writeln('fail'); end;  procedure swapem; var   tmp: char; begin   tmp := decimalseparator;   decimalseparator := thousandseparator;   thousandseparator := tmp; end;  begin   test('default');   swapem;   test('changed');   readln; end. 

the first test works fine, second 1 fails.

is there way make variant conversion use sysutils.decimalseparator , sysutils.thousandseparator?

you can replace varr8fromstr function in varutils.pas liking, , vartodoubleasstring use instead:

function myconversion(const strin: widestring; lcid: integer; dwflags: longint;     out dblout: double): hresult; stdcall; const   cresult: array [false..true] of hresult = (var_invalidarg, var_ok); var   s: string; begin   s := stringreplace(strin, thousandseparator, '', [rfreplaceall]);   result := cresult[trystrtofloat(s, dblout)]; end;  [...]  begin   varutils.varr8fromstr := myconversion;   [...] 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -