Search code examples
delphivariantdecimal-point

Cast from Variant String to Double ignores decimal point


I have a few simple lines of code:

var
  vRecordValue:Double;
begin
  vRecordValue:= someVariant;

Where someVariant is a string (type 256).

On a production system we have a variant of '23.4' and vRecordValue becomes 234. I can only reproduce this is my system separator is ','. In our production system this was the case but we have changed the setting and still see this occurring. It is interesting to note that we cannot reproduce this at all except in debug mode (no idea what the relationship is here), and by doing a simple test.

I fixed this with an explicit conversion:

vRecordValue:= StrToFloatDef(VarToStrDef(someVariant, '0'), 0);

Questions:

  1. Is there any other way that a decimal point could be being ignored?
  2. Can someone please provide a reference for how the default cast from string to double is performed? I want to understand the difference.
  3. Is there any way that the separator used in the default cast is being cached from the value when the code was first run? ... Doubtful but desperate to understand.

Thanks,

Wayne.


Solution

  • It is your regional settings

    I've made this samall demo applikation inorder for testing it:

    procedure TForm60.FormCreate(Sender: TObject);
    var
      vRecordValue: Double;
      sRecordValue: Variant;
    begin
      sRecordValue:= '23.4';
      vRecordValue:= StrToFloatDef(VarToStrDef(sRecordValue, '0'), 0);
      ShowMessage(FloatToStr(vRecordValue));
    
      sRecordValue:= '23,4';
      vRecordValue:= StrToFloatDef(VarToStrDef(sRecordValue, '0'), 0);
      ShowMessage(FloatToStr(vRecordValue));
    end;
    

    In Denmark (Where I live) the we use , as decimal seperator, so the last one gives me the correct result