简体   繁体   English

从Variant String转换为Double忽略小数点

[英]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). someVariant是一个字符串(类型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 ','. 在生产系统上,我们有'23 .4'的变体,vRecordValue变为234.我只能重现这是我的系统分隔符是','。 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. 韦恩。

It is your regional settings 这是您的区域设置

I've made this samall demo applikation inorder for testing it: 为了测试它,我做了这个samall演示应用程序:

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 在丹麦(我住的地方)我们使用,作为十进制分隔符,所以最后一个给我正确的结果

In regards to my third question. 关于我的第三个问题。 The problem reoccurred this week. 这个问题本周再次出现。 I didn't realize that the locale/regional settings are only being set for the current user. 我没有意识到只为当前用户设置了区域设置/区域设置。

To copy the settings for system users, we needed to use: 要复制系统用户的设置,我们需要使用:

Control Panel > Region and Language > Administrative > Copy Settings. 控制面板>区域和语言>管理>复制设置。

This is why I had inconsistencies when testing. 这就是我在测试时遇到不一致的原因。 It depended on the user my application was running as, and that users settings. 它取决于我的应用程序运行的用户和用户设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM