简体   繁体   English

我需要在 Groovy 中创建一个 function ,它有一个 integer 作为参数并返回它包含的有效数字的数量

[英]I need to create a function in Groovy that has a single integer as a parameter and returns the number of significant figures it contains

Long story short, I'm working in a system that only works with groovy in its expression editor, and I need to create a function that returns the number of significant figures an integer has.长话短说,我在一个仅在其表达式编辑器中使用 groovy 的系统中工作,我需要创建一个 function 来返回 Z157DB7DF530023575515D366C9B672 具有的有效数字的数量。 I've found the following function in stack overflow for Java, however it doesnt seem like groovy (or the system itself) likes the regex:我在 Java 的堆栈溢出中发现了以下 function,但它似乎不像 groovy(或系统本身)喜欢正则表达式:

String myfloat = "0.0120";

String [] sig_figs = myfloat.split("(^0+(\\.?)0*|(~\\.)0+$|\\.)");

int sum = 0;

for (String fig : sig_figs)
{
    sum += fig.length();
}

return sum;

I've since tried to convert it into a more Groovy-esque syntax to be compatible, and have produced the following:从那以后,我尝试将其转换为更具 Groovy 风格的语法以兼容,并产生了以下内容:

def sum = 0;

def myint = toString(mynum);

def String[] sig_figs = myint.split(/[^0+(\\.?)0*|(~\\.)0+$|\\.]/);

for (int i = 0; i <= sig_figs.size();i++)
{
    sum += sig_figs[i].length();
}
return(sum); 

It should also be noted that this system has very little visibility in regards to what groovy functions are available in the system, so the solution likely needs to be as basic as possible还应注意,该系统对于系统中可用的 groovy 功能几乎没有可见性,因此解决方案可能需要尽可能基本

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

I think this is the regex you need:我认为这是您需要的正则表达式:

def num = '0.0120'
def splitted = num.split(/(^0+(\.?)0*|(~\.)0+$|\.)/)
def sf = splitted*.length().sum()​​​​​​​​​​​​​​​​​​​​​

It's been a while since I've had to think about significant figures, so sorry if I have the wrong idea.自从我不得不考虑重要数字以来已经有一段时间了,如果我有错误的想法,很抱歉。 But I've made two regular expressions that combined should count the number of significant figures (sorry I'm no regex wizard) in a string representing a decimal.但是我已经做了两个正则表达式,它们组合起来应该计算一个表示小数的字符串中的有效数字的数量(对不起,我不是正则表达式向导)。 It doesn't handle commas, you would have to strip those out.它不处理逗号,你必须把它们去掉。

This first regex matches all significant figures before the decimal point第一个正则表达式匹配小数点前的所有有效数字

([1-9]+\d*[1-9]|[1-9]+)

And this second regex matches all significant figures after the decimal point:第二个正则表达式匹配小数点后的所有有效数字:

\.((\d*[1-9]+)+)?

If you add up the lengths of the first capture group (or 0 when no match) for both matches, then it should give you the number of significant figures.如果将两个匹配项的第一个捕获组的长度相加(如果不匹配,则为 0),那么它应该为您提供有效数字的数量。

Example:例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SigFigs {

    private static final Pattern pattern1 = Pattern.compile("([1-9]+\\d*[1-9]|[1-9]+)");
    private static final Pattern pattern2 = Pattern.compile("\\.((\\d*[1-9]+)+)?");

    public static int getSignificantFigures(String number) {
        int sigFigs = 0;
        for (int i=0; i < 2; i++) {
            Matcher matcher = (i == 0 ? pattern1 : pattern2).matcher(number);
            if (matcher.find()) {
                try {
                    String s = matcher.group(1);
                    if (s != null) sigFigs += s.length();
                } catch (IndexOutOfBoundsException ignored) { }
            }
        }
        return sigFigs;
    }
    
    public static void main(String[] args) {
        System.out.println(getSignificantFigures("0305.44090")); // 7 sig. figs
    }

}

Of course using two matches is suboptimal (like I've said, I'm not crazy good at regex like some I could mention) but its fairly robust and readable当然,使用两个匹配是次优的(就像我说过的,我并不像我可能提到的那样非常擅长正则表达式),但它相当健壮且可读

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

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