{"record":{"id":"facaa43b9ba2a5fe","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-single","errorCode":null,"errorMessage":"Can not convert {0} to Single.","messagePattern":"Can not convert (.+?) to Single\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1154,"sourceCode":"            return (v.Value != null) ? (long?)Convert.ToInt64(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=\"Single\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator float?(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 Single.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (float?)integer;\n            }\n#endif\n\n            return (v.Value != null) ? (float?)Convert.ToSingle(v.Value, CultureInfo.InvariantCulture) : null;\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Decimal\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator decimal(JToken value)","sourceCodeStart":1136,"sourceCodeEnd":1172,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1136-L1172","documentation":"Thrown by the explicit cast operator 'operator float?(JToken?)' (Nullable<Single>) when the token is not resolvable to a numeric JValue. ArgumentException {0} is the resolved JTokenType; allowed types are NumberTypes ({Integer, Float, String, Comment, Raw, Boolean}). Nullable overload, so null/JSON-null return null; the throw indicates a structurally incompatible token (e.g. Date, Guid, Object, Array).","triggerScenarios":"Calling (float?)token[\"rate\"] where the resolved JTokenType is Date, Guid, TimeSpan, Uri, Bytes, Object, or Array, or the node is a JObject/JArray (EnsureValue null). A non-numeric string like \"n/a\" passes ValidateToken but then throws FormatException from Convert.ToSingle, not this error.","commonSituations":"Percentage/rate field replaced by an object (e.g. { \"value\": 1.5, \"unit\": \"%\" }); a null-bearing field indexed into the wrong node; third-party payload that sometimes embeds metadata objects where a scalar float is expected.","solutions":["Guard with token[\"rate\"]?.Type in {Integer, Float, String, Boolean} before the cast.","Prefer token[\"rate\"]?.Value<float>() returning 0/null on mismatch instead of throwing.","If the field became a structured object, parse the nested scalar (e.g. payload[\"rate\"][\"value\"]) instead.","Deserialize into a typed POCO so contract drift surfaces as clear mapping errors."],"exampleFix":"// before\nfloat? rate = (float?)payload[\"rate\"]; // throws when rate is an object\n\n// after\nvar rateNode = payload[\"rate\"];\nfloat? rate = (rateNode is JValue jv && (jv.Type == JTokenType.Float || jv.Type == JTokenType.Integer)) ? (float?)jv : null;","handlingStrategy":"validation","validationCode":"float? rate = (payload[\"rate\"] is JValue jv &&\n    (jv.Type == JTokenType.Float || jv.Type == JTokenType.Integer)) ? (float?)jv : null;","typeGuard":"static bool IsNumericLike(JToken? t) =>\n    t is JValue v && Array.IndexOf(\n        new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Boolean }, v.Type) >= 0;","tryCatchPattern":"float? rate;\ntry { rate = (float?)payload[\"rate\"]; }\ncatch (ArgumentException) { rate = null; }","preventionTips":["Use token[\"rate\"]?.Value<float>() to avoid throws on object/structured values.","If the rate is wrapped in an object, parse the nested scalar.","Guard on JTokenType before casting untrusted JSON."],"tags":["json","linq-to-json","type-cast","jtoken","numeric","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}