MCPcopy Index your code
hub / github.com/ccxt/ccxt / toDouble

Method toDouble

java/lib/src/main/java/io/github/ccxt/base/Generic.java:20–42  ·  view source on GitHub ↗
(Object o)

Source from the content-addressed store, hash-verified

18 }
19
20 public static double toDouble(Object o) {
21 if (o == null) return 0.0;
22 if (o instanceof Number n) return n.doubleValue();
23 // JS coerces booleans to 0/1 in arithmetic (e.g. `true + 1 == 2`).
24 // bitget.createOrderRequest does `this.sum(isTriggerOrder, isStopLoss…) > 1`
25 // with boolean inputs; Java's strict typing means we have to handle the
26 // bool→double coercion ourselves here, otherwise String.valueOf(false)
27 // → "false" → Double.parseDouble throws NumberFormatException.
28 if (o instanceof Boolean b) return b ? 1.0 : 0.0;
29 // TS's `sum` and similar arithmetics filter non-numbers via isNumber
30 // and silently return undefined/NaN — they don't throw on a Map or
31 // List or arbitrary object. Java's strict parseDouble does. Mirror
32 // the TS semantics by returning 0.0 (matches TS's `Number(NaN) || 0`
33 // pattern) when the input isn't parseable as a number. Without this,
34 // a single misuse of `sum` in transpiled code (e.g. bitmex passing
35 // the whole counter map instead of a single counter) tears down the
36 // entire WS connection on the first bad frame.
37 try {
38 return Double.parseDouble(String.valueOf(o));
39 } catch (NumberFormatException ex) {
40 return 0.0;
41 }
42 }
43
44 // ---------- sortBy ----------
45

Callers 1

sumMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected