检查字符串是否在 Java 中有小数部分:涉及语言环境

Checking if a String has a fraction part in Java : Locale involved

我有一个 String 值,它可以容纳 LongDoubleString 可能基于 Locale

因此它可能包含以下值:

11
11.00
15,25 (for Locale like Denmark where the decimal part is denoted by a comma instead of dot)

我只想做一个Double;从某种意义上说,它包含一个分数值。分数值“00”也是有效的情况。

if(string contains fraction){
  // do something
}

给出以上三个示例,对于 11.0015,25,控制应该进入 if 而不是对于 11。

我该如何检查?

请记住涉及语言环境。所以点和逗号对于不同的 Locale 可能有不同的含义。如此简单的正则表达式来查找它们的出现是行不通的。例如如果 Locale 是 Australia,则 11,00 是 1100,因此不是双精度数。但如果 Locale 是欧洲国家,如丹麦或德国,则 11,00 是双倍数。

我需要使用 NumberFormat 找到一些解决方案,但无法解决。

我有语言环境信息。所以我知道字符串是否属于哪个区域设置。鉴于此,我如何才能找到 String 是否有小数?

使用正则表达式你可以做到

Pattern decimalPattern = Pattern.compile("\d+(,|\.)\d+{2}");

然后有

boolean isDecimal = decimalPattern.matcher(input).matches();

正则表达式:

  • \d+一位或多位
  • (,|\.)小数点或逗号
  • \d+又是一位或多位数字

或者你可以做拆分的事情

String[] split = input.split("(,|\.)");
boolean isDecimal = split.length > 1 && split[1].length() == 2;

您可以使用循环来检查它是否有逗号或点,然后使用 if?

检查
boolean IsADouble = false;
for (int i = 0; i < String.length(); i++) {
            if (String.charAt(i) == ','|| String.charAt(i) == '.') {
                IsADouble = true
            }
        }

然后你创建 If 来做一些事情,如果它是双精度的。

希望对您有所帮助:)

编辑:由于您已经编辑了说明您知道区域设置的问题,因此您可以将其与 NumberFormat.getNumberInstance(locale).parse(strValue) 结合用于逗号和千位分隔符的正则表达式。这里有一个测试代码:

import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

class Main{
  private static final Locale DUTCH = new Locale("nl","NL");

  public static void main(String[] a){
    test("11", Locale.ENGLISH);
    test("11", DUTCH);
    System.out.println();
    test("11.00", Locale.ENGLISH);
    test("11.00", DUTCH);
    System.out.println();
    test("11,00", Locale.ENGLISH);
    test("11,00", DUTCH);
    System.out.println();
    test("15.123", Locale.ENGLISH);
    test("15.123", DUTCH);
    System.out.println();
    test("15,123", Locale.ENGLISH);
    test("15,123", DUTCH);
    System.out.println();
    test("something", Locale.ENGLISH);
    test("something", DUTCH);
  }

  static void test(String val, Locale locale){
    try{
      DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
      char decimalSep = symbols.getDecimalSeparator();
      char thousandSep = symbols.getGroupingSeparator();

      String escapedDecimalSep = decimalSep == '.' ? "\." : decimalSep+"";
      String escapedThousandSep = thousandSep == '.' ? "\." : thousandSep+"";

      String intRegex = "\d+(" + escapedThousandSep + "\d{3})*"; // Example ENGLISH: "\d+(,\d{3})*"
      String doubleRegex = intRegex + escapedDecimalSep + "\d+"; // Example ENGLISH: "\d+(,\d{3})*\.\d+"

      NumberFormat format = NumberFormat.getInstance(locale);
      Number number = format.parse(val);
      if(val.matches(doubleRegex)){
        double d = number.doubleValue();
        System.out.println(val + " (in locale " + locale + ") is a double: " + d);
      } else if(val.matches(intRegex)){
        int i = number.intValue();
        System.out.println(val + " (in locale " + locale + ") is an integer: " + i);
      } else{
        System.out.println("Unable to determine whether value " + val + " is an integer or double for locale " + locale);
      }
    } catch(ParseException ex){
      System.out.println("Error occurred for value \"" + val + "\". Are you sure it's an integer or decimal?");
    }
  }
}

Try it online.

这是输出:

11 (in locale en) is an integer: 11
11 (in locale nl_NL) is an integer: 11

11.00 (in locale en) is a double: 11.0
Unable to determine whether value 11.00 is an integer or double for locale nl_NL

Unable to determine whether value 11,00 is an integer or double for locale en
11,00 (in locale nl_NL) is a double: 11.0

15.123 (in locale en) is a double: 15.123
15.123 (in locale nl_NL) is an integer: 15123

15,123 (in locale en) is an integer: 15123
15,123 (in locale nl_NL) is a double: 15.123

Error occurred for value "something". Are you sure it's an integer or decimal?
Error occurred for value "something". Are you sure it's an integer or decimal?