检测字符串是否包含浮点数? [英] Detect if string contains a float?

查看:86
本文介绍了检测字符串是否包含浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测字符串是否包含浮点数。例如:'0.004'

How can I detect if a string contains a float. For example: '0.004'

但不使用 StrToFloat ,因为该函数很慢,但是通过对char进行迭代来实现。

But without using StrToFloat because that function are slow but rather by iterating through chars.

function IsInteger(const S: String): Boolean;
var
  P: PChar;
begin
  P := PChar(S);
  Result := True;
  while not (P^ = #0) do
  begin
    case P^ of
      '0'..'9': Inc(P);
    else
      Result := False;
      Break;
    end;
  end;
end;

这将检查字符串是否为正整数而不是浮点数。

This will check if string is a positive integer but not a float..

推荐答案

我将使用TryStrToFloat():

I would use TryStrToFloat():

if TryStrToFloat(str, value, FormatSettings) then
  ....

准备使用默认的系统范围格式设置,则可以省略最后一个参数:

If you are prepared to use the default system wide format settings then you can omit the final parameter:

if TryStrToFloat(str, value) then
  ....

这篇关于检测字符串是否包含浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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