AndroidAPS/app/src/main/java/info/nightscout/utils/SafeParse.java

50 lines
1.3 KiB
Java
Raw Normal View History

2016-06-23 17:07:38 +02:00
package info.nightscout.utils;
2017-03-05 20:12:30 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2016-06-23 17:07:38 +02:00
/**
* Created by mike on 23.06.2016.
*/
public class SafeParse {
2017-03-05 20:12:30 +01:00
private static Logger log = LoggerFactory.getLogger(SafeParse.class);
2016-06-23 17:07:38 +02:00
public static Double stringToDouble(String input) {
Double result = 0d;
input = input.replace(",", ".");
2017-06-20 19:58:43 +02:00
if (input.equals(""))
return 0d;
2016-06-23 17:07:38 +02:00
try {
result = Double.parseDouble(input);
} catch (Exception e) {
2017-03-05 20:12:30 +01:00
log.error("Error parsing " + input + " to double");
2016-06-23 17:07:38 +02:00
}
return result;
}
public static Integer stringToInt(String input) {
Integer result = 0;
input = input.replace(",", ".");
2017-06-20 19:58:43 +02:00
if (input.equals(""))
return 0;
2016-06-23 17:07:38 +02:00
try {
result = Integer.parseInt(input);
} catch (Exception e) {
2017-03-05 20:12:30 +01:00
log.error("Error parsing " + input + " to int");
2016-06-23 17:07:38 +02:00
}
return result;
}
2016-08-07 13:07:13 +02:00
public static Long stringToLong(String input) {
Long result = 0L;
input = input.replace(",", ".");
2017-06-20 19:58:43 +02:00
if (input.equals(""))
return 0L;
2016-08-07 13:07:13 +02:00
try {
result = Long.parseLong(input);
} catch (Exception e) {
2017-03-05 20:12:30 +01:00
log.error("Error parsing " + input + " to long");
2016-08-07 13:07:13 +02:00
}
return result;
}
2016-06-23 17:07:38 +02:00
}