{"record":{"id":"792ad89c44f6cdb3","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-double","errorCode":null,"errorMessage":"Can not convert {0} to Double.","messagePattern":"Can not convert (.+?) to Double\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":764,"sourceCode":"            return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"Double\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator double?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, NumberTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to Double.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (double?)integer;\n            }\n#endif\n\n            return (v.Value != null) ? (double?)Convert.ToDouble(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"Char\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator char?(JToken? value)","sourceCodeStart":746,"sourceCodeEnd":782,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L746-L782","documentation":"Thrown by the explicit nullable cast operator double?(JToken?) at JToken.cs:764. With nullable:true, JSON null/undefined tokens pass and return null. The error fires when the token is not a JValue or its type is outside NumberTypes = {Integer, Float, String, Comment, Raw, Boolean} — e.g. Date, Guid, Uri, Bytes, Object, Array.","triggerScenarios":"Casting a Date/Guid/Uri JValue or a JObject/JArray to (double?)token; a measurement field whose Type is Date or Object. A JSON null returns null.","commonSituations":"A numeric measurement field came back as a nested object or date; a Guid/Uri value read as a double; a field re-typed by an upstream service.","solutions":["Guard with token.Type in {Integer, Float, String, Boolean} before the cast.","Use token.Value<double?>() which returns default for incompatible tokens.","Inspect the JSON to confirm a scalar number, not a container, is present.","Deserialize into a nullable double POCO property."],"exampleFix":"// before\ndouble? v = (double?)token[\"value\"]; // throws when value is a date\n\n// after\ndouble? v = (token[\"value\"]?.Type is JTokenType.Integer or JTokenType.Float)\n    ? (double?)token[\"value\"]\n    : null;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a number-compatible scalar (nullable cast tolerates JSON null)\nstatic bool CanToNullableDouble(JToken? t) =>\n    t is not JValue v || v.Type is JTokenType.Integer or JTokenType.Float\n        or JTokenType.String or JTokenType.Boolean or JTokenType.Null or JTokenType.Undefined;","typeGuard":"static double? SafeDouble(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String or JTokenType.Boolean => (double?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null\n};","tryCatchPattern":"try\n{\n    double? d = (double?)token;\n}\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not convert\"))\n{\n    // token is a container or Date/Guid; default to null\n}","preventionTips":["Check token.Type is Integer/Float before casting.","Use token.Value<double?>() for tolerant default handling.","Confirm the field is a numeric scalar, not a nested object/date.","Deserialize into a nullable double POCO property."],"tags":["linq-to-json","jtoken","cast","nullable-double","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}