安全地解析数字并且敏感地区域化 [英] Parsing numbers safely and locale-sensitively

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

问题描述

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天全站免登陆