安全且区分区域设置的解析数字 [英] Parsing numbers safely and locale-sensitively

查看:24
本文介绍了安全且区分区域设置的解析数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 的 NumberFormat 是 1) 非线程安全的(可以使用 ThreadLocal 解决);2) 不方便正确使用最简单的用例当我知道字符串是否应该包含 int、long 或 double 并且想要一个 API 时:

Java's NumberFormat is 1) non thread-safe (which can be worked around with a ThreadLocal); 2) inconvenient to use correctly for the simplest use case when I know whether the string should contain int, long, or double, and want an API like:

int parseInt(String str, Locale locale) throws ParseException; 
int parseInt(String str, int defaultValue, Locale locale);
long parseLong(String str, Locale locale) throws ParseException;
long parseLong(String str, long defaultValue, Locale locale);
double parseDouble(String str, Locale locale) throws ParseException;
double parseDouble(String str, double defaultValue, Locale locale);

字符串未完全解析时抛出异常的地方.显然,这样的包装器很容易编写,但我在 Guava 或 Apache Commons Lang 中找不到.我只是错过了吗?或者是否有其他或多或少的标准解决方案?

where the exception is thrown when the string isn't completely parsed. Obviously, such a wrapper is easy to write, but I couldn't find one in Guava or Apache Commons Lang. Did I just miss it? Or is there another more-or-less standard solution for this?

推荐答案

我发现的一个选项是 Apache Commons Validator(仍然是非线程安全的).

One option I've found is Apache Commons Validator (still non-thread-safe).

这篇关于安全且区分区域设置的解析数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆