我有一个String
值,它可以保存Long
或Double
。 String
可能是基于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.00
和 15,25
,控制应该放在 if
内部,但对于 11,则不应该。
我怎样才能检查这个?
请记住,涉及区域设置。因此点和逗号对于不同的区域设置可能有不同的含义。因此,简单的正则表达式来查找它们的出现是行不通的。例如如果区域设置为澳大利亚,则 11,00 为 1100,因此不是 double 。但如果区域设置是丹麦或德国等欧洲国家,则 11,00 是双倍。
我需要使用 NumberFormat
找到一些解决方案,但无法解决。
我有区域设置信息。所以我知道字符串是否属于哪个区域设置。鉴于此,我怎样才能知道字符串是否有分数?
最佳答案
编辑:由于您已经编辑了您的问题,表明您知道区域设置,因此您可以将其与 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?");
}
}
}
这是输出:
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?
关于java - Java中检查字符串是否有小数部分 : Locale involved,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51627511/