MCPcopy Create free account
hub / github.com/dynjs/dynjs / stringToNumber

Method stringToNumber

src/main/java/org/dynjs/runtime/Types.java:216–263  ·  view source on GitHub ↗
(String str)

Source from the content-addressed store, hash-verified

214
215 static final Pattern decimalDigitPattern = Pattern.compile(".*[0-9].*");
216 public static Number stringToNumber(String str) {
217 str = trimNumericString(str);
218
219 if (str.equals("")) {
220 return 0L;
221 }
222
223 if (str.equals("-0")) {
224 return Double.valueOf(-0.0);
225 }
226
227 if (str.equals("Infinity") || str.equals("+Infinity")) {
228 return Math.pow(10, 10000);
229 }
230
231 if (str.equals("-Infinity")) {
232 return -Math.pow(10, 10000);
233 }
234
235 if (str.startsWith("0x") || str.startsWith("0X")) {
236 return Long.decode(str);
237 }
238
239 // If we've come this far and there's not a decimal digit in
240 // this string anywhere then bail out early
241 if (!decimalDigitPattern.matcher(str).matches()) {
242 return Double.NaN;
243 }
244
245 if ((str.indexOf("e") > 0) || (str.indexOf("E") > 0)) {
246 if (str.indexOf(".") >= 0) {
247 str = str.replaceFirst("[eE]", "E");
248 } else {
249 str = str.replaceFirst("[eE]", ".0E");
250 }
251 }
252
253 try {
254 if (str.indexOf(".") >= 0) {
255 return Double.valueOf(str);
256 } else {
257 return Long.valueOf(str);
258 }
259 } catch (NumberFormatException e) {
260 return Double.NaN;
261 }
262
263 }
264
265 public static Boolean toBoolean(Object o) {
266 // 9.2

Callers 1

toNumberMethod · 0.95

Calls 2

trimNumericStringMethod · 0.95
decodeMethod · 0.80

Tested by

no test coverage detected