I have tried to parse the maxvalue of long and ulong but it fails. How to do that?
Int64.TryParse(value.ToString(), out resultInt)
value
is Int64.MaxValue but the type of the value
is double.
I have tried to parse the maxvalue of long and ulong but it fails. How to do that?
Int64.TryParse(value.ToString(), out resultInt)
value
is Int64.MaxValue but the type of the value
is double.
Copyright © 2021 Jogjafile Inc.
What's happening
Since
double
has the precision of ~15-17 digits it's not possible to stringify adouble
that was assignedInt64.MaxValue
in a way that the string would represent exactly9223372036854775807
(Int64.MaxValue
, 19 digits).Solution
A. Use
Int64
Don't store integral values as
double
.B. Fudge
With
ToString("F0")
(and few other formats) it's possible to get quite close; just 1 aboveInt64.MaxValue
(9223372036854775808
).You could try to trim the double string.
Test code
Output
Examples of different formats
Here are some examples for few
ToString
formats.Output