用逗号将字符串转换为float [英] Convert string with commas to float

查看:249
本文介绍了用逗号将字符串转换为float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个内置的Delphi函数,它将一个字符串(例如'3,232.00')转换为float?由于逗号,StrToFloat会引发异常。还是唯一先删除逗号的方法,然后执行StrToFloat?



谢谢。

解决方案

你完全知道,'。'是小数分隔符,','是千分隔符(总是)
如果是这样,那么你应该填写TFormatSettings记录并传递给StrToFloat。

  FillChar(FS,SizeOf FS),0); 
... //填充其他字段
FS.ThousandSeparator:=',';
FS.DecimalSeparator:='。';
V:= StrToFloat(S,FS);


Is there a built-in Delphi function which would convert a string such as '3,232.00' to float? StrToFloat raises an exception because of the comma. Or is the only way to strip out the comma first and then do StrToFloat?

Thanks.

解决方案

Do you exactly know, that '.' is decimal separator and ',' is thousand separator (always)? If so, then you should fill the TFormatSettings record and pass it to StrToFloat.

FillChar(FS, SizeOf(FS), 0);
... // filling other fields
FS.ThousandSeparator := ',';
FS.DecimalSeparator := '.';
V := StrToFloat(S, FS);

这篇关于用逗号将字符串转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆