{"record":{"id":"0485452fce72caa9","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-decimal","errorCode":null,"errorMessage":"Can not convert {0} to Decimal.","messagePattern":"Can not convert (.+?) to Decimal\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":736,"sourceCode":"        }\n#endif\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <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)\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 Decimal.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return (decimal?)integer;\n            }\n#endif\n\n            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)","sourceCodeStart":718,"sourceCodeEnd":754,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L718-L754","documentation":"Thrown by the explicit nullable cast operator decimal?(JToken?) at JToken.cs:736. With nullable:true, JSON null/undefined tokens pass and return null. The ArgumentException is raised when the token is not a JValue (container) 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/Bytes JValue or a JObject/JArray to (decimal?)token; a token whose Type is Date where a currency amount was expected. A JSON null returns null.","commonSituations":"A monetary field represented as a string-ified object or as a Date; a field re-typed to an object after an API change; reading a Guid identifier as a decimal amount.","solutions":["Check token.Type is Integer, Float, or String before the cast.","Use token.Value<decimal?>() to get default for incompatible tokens.","Confirm the JSON carries a numeric (or numeric-string) scalar, not a nested object.","Deserialize into a POCO with a nullable decimal property for schema enforcement."],"exampleFix":"// before\ndecimal? amount = (decimal?)token[\"amount\"]; // throws when amount is an object\n\n// after\ndecimal? amount = (token[\"amount\"]?.Type is JTokenType.Integer or JTokenType.Float or JTokenType.String)\n    ? (decimal?)token[\"amount\"]\n    : null;","handlingStrategy":"type-guard","validationCode":"// Verify the token is a number-compatible scalar (nullable cast tolerates JSON null)\nstatic bool CanToNullableDecimal(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 decimal? SafeDecimal(JToken? t) => t switch {\n    JValue v when v.Type is JTokenType.Integer or JTokenType.Float\n                       or JTokenType.String or JTokenType.Boolean => (decimal?)t,\n    JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,\n    _ => null // container or Date/Guid/etc.\n};","tryCatchPattern":"try\n{\n    decimal? d = (decimal?)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/String before casting.","Use token.Value<decimal?>() for tolerant default handling.","Confirm the field is a numeric scalar, not a nested object.","Deserialize into a nullable decimal POCO property."],"tags":["linq-to-json","jtoken","cast","nullable-decimal","argumentexception"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}