Android德语环境下处理小数点时兼容异常处理
public class Demo{
private DecimalFormat decimalFormat = new DecimalFormat("0.0000");
private void handlerScore() {
String format = decimalFormat.format(23.23232332);
getScore(format);
}
private float getScore(String score) {
float result = Float.parseFloat(score);
Log.d(TAG, "getScore: =" + result);
return result;
}
}
在中文环境下运行正常,但是在德语环境下运行crash,crash信息如下:
Caused by: java.lang.NumberFormatException: For input string: "23,2323"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at com.xiaoma.testc.MainActivity.getScore(MainActivity.java:39)
at com.xiaoma.testc.MainActivity.handlerScore(MainActivity.java:33)
分析:
是因为DecimalFormat处理小数位数时变更为23,2323; 中间的【.】变成了【,】,导致了Float.parseFloat(**)报错
解决方案:
private void handlerScore() {
String format = decimalFormat.format(23.23232332);
getScore(format);
}
private float getScore(String score) {
float result;
try {
result = Float.parseFloat(score);
} catch (NumberFormatException exception) {
String newResult = score.replace(",", ".");
result = getScore(newResult);
}
Log.d(TAG, "getScore: =" + result);
return result;
}
其他方案
由于我的项目里返回的值23.23232332是第三方返回的,所以让第三方修改小数位数; 这个可以根据项目调整;
总结
对于掉用系统Api还是需要关注api相关说明,防治潜在的问题。
1、Float做parseFloat(**)处理时抛出异常
package java.lang;
public final class Float extends Number implements Comparable<Float> {
/**
* Returns a new {@code float} initialized to the value
* represented by the specified {@code String}, as performed
* by the {@code valueOf} method of class {@code Float}.
*
* @param s the string to be parsed.
* @return the {@code float} value represented by the string
* argument.
* @throws NullPointerException if the string is null
* @throws NumberFormatException if the string does not contain a
* parsable {@code float}.
* @see java.lang.Float#valueOf(String)
* @since 1.2
*/
public static float parseFloat(String s) throws NumberFormatException {
return FloatingDecimal.parseFloat(s);
}
}
2、Geocoder根据经纬度获取地址列表时是耗时操作
package android.location;
public final class Geocoder {
/**
* Returns an array of Addresses that attempt to describe the area immediately surrounding the
* given latitude and longitude. The returned addresses should be localized for the locale
* provided to this class's constructor. Results may be obtained by means of a network lookup
* and this method may take some time to return, and so should not be called on the main thread.
*
* Warning: Geocoding services may provide no guarantees on
* availability or accuracy. Results are a best guess, and are not guaranteed to be meaningful
* or correct. Do NOT use this API for any safety-critical or regulatory compliance
* purposes.
*
* @param latitude the latitude a point for the search
* @param longitude the longitude a point for the search
* @param maxResults max number of addresses to return. Smaller numbers (1 to 5) are recommended
*
* @return a list of Address objects. Returns null or empty list if no matches were
* found or there is no backend service available.
*
* @throws IllegalArgumentException if latitude is
* less than -90 or greater than 90
* @throws IllegalArgumentException if longitude is
* less than -180 or greater than 180
* @throws IOException if the network is unavailable or any other
* I/O problem occurs
*/
public List<Address> getFromLocation(double latitude, double longitude, int maxResults)
throws IOException {
Preconditions.checkArgumentInRange(latitude, -90.0, 90.0, "latitude");
Preconditions.checkArgumentInRange(longitude, -180.0, 180.0, "longitude");
try {
GeocodeListener listener = new GeocodeListener();
mService.getFromLocation(latitude, longitude, maxResults, mParams, listener);
return listener.getResults();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
}