java判断字符串是否有小数,检查字符串是否包含Java:Locale中的小数部分

编辑:由于您已经编辑了说明您知道区域设置的问题,因此可以将其与

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值