首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查是否可以在NX知识融合中将字符串转换为数字类型

检查是否可以在NX知识融合中将字符串转换为数字类型
EN

Stack Overflow用户
提问于 2015-12-01 08:30:15
回答 2查看 809关注 0票数 0

我有一个NX知识融合检查伴侣程序,以检查是否有任何手动尺寸包含任何类型的数字。

但是维度的数据类型总是字符串列表(我只对列表的第一个位置感兴趣)。

如果包含在列表第一个位置的字符串可以转换为数字类型,即如果字符串是"200“,程序必须返回true并将维度标记附加到列表中,但如果是"22c”,则程序必须返回false。

我尝试了函数MakeNumber( string ),但是只有当字符串可以转换为Number时,这个函数才能工作。如果字符串不是数字,程序就会崩溃。

非常感谢!

我的检查程序函数的dfa代码是:

检验函数

(任何未缓存的) do_check:@{ $dim_manual << mqc_askManualDimensions();

代码语言:javascript
复制
          $dim_log <<loop
   {
       for $each in $dim_manual;
       for $is_sleep is mqc_isSleepObject( $each );
       for $is_condemned is mqc_isCondemnedObject( $each );

       #I print the value i want to check
       do ug_printvalue($each);
       for $text is mqc_askDimensionManualText( $each );
       do ug_printvalue(nth(0,$text));

       #I check the type of the dimension content --> String
       #If nth(0,$text) is Number type, typecheck returns true
       #but always return false because nth(0,$text) is String type!
       for $is_number is typecheck(nth(0,$text), Number);
       do ug_printvalue($is_number);
       do ug_printvalue(TypeName(nth(0,$text)));

       #I try to convert the String in a Number. If the String can not be
       #converted the program crashes!!
       for  $n  is MakeNumber(nth(0,$text)); 
        do ug_printvalue($n);         

      #I want to append in the error log only the manual dimensions that
      #contains ONLY a number.
      if (!$is_sleep & !$is_condemned & $is_number)
       append {$each};
         };

   if !empty?( $dim_log ) Then
   @{
       $log_msg << @{If (log_msg:="") Then "" Else log_msg:+"~n";} +
                   mqc_sprintf("Found %s dimension(s) with manual text.", Stringvalue(Length($dim_log)));
       ug_mqc_log( nth( log_type:, log_type_option: ), $dim_log, $log_msg );
   }
   Else donothing;    
};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-02 15:55:58

我找到了解决办法。我的dfa方法,该方法确定是否可以将给定的字符串解析为数字。

代码语言:javascript
复制
   ( List )   numbers: {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "." , ","};
   ( Method Boolean)   vh_numeric_string:(String $cadena) @{
         $is_a_number << loop {
             for $text_split is SplitString($cadena,"");
               for $i from 1 to Length($text_split);
                 for $exist_number is Find(nth($i,$text_split),numbers:);

          if ($exist_number = NoValue) return False;
          return is True;
       };
  };

当在dfa文件的do_check函数中调用此方法时,结果可以存储在循环变量中,如下所示:

代码语言:javascript
复制
for $is_number is vh_numeric_string:(nth(0,$text));
票数 0
EN

Stack Overflow用户

发布于 2015-12-01 15:06:34

我编写了一个计算字符串是否为数字的方法。

代码语言:javascript
复制
(List)  numbers: {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "." , ","};

(Method Boolean) is_numeric:(String $cadena)
    @{
        $is_a_number << loop {
        for $text_split is SplitString($cadena,"");
    do ug_printMessage($text_split);
        for $i from 0 to Length($text_split);

        for $exist_number is Find(nth($i,$text_split),numbers:);
        do ug_printvalue($i);
        do ug_printvalue($exist_number);

       return is $exist_number != "No Value";
   };
};

此方法被声明为类的一个属性,只有在数字列表中可以找到$cadena的所有字符(逗号和点已作为可能的十进制分隔符)时,才应该返回true。

但是现在我不知道如何在NX Check mate类中执行这个方法。谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34015854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档