What is the best way to convert/parse a string into a ulong in c# and keep precision ?
Direct cast is not possible, and the Convert class utility is not providing ulong conversion,
so I used an intermediate decimal variable, but I am losing precision.
decimal d = Decimal.Parse("1.0316584"));
Console.Write(d) // displays 1.0316584
ulong u = (ulong)d;
Console.Write(u) // displays 1 , precision is lost
I first tried to use a long parser, but I got thrown out :
long l = Int64.Parse("1.0316584")); // throws System.FormatException
EDIT :
Ok sorry my bad : My question was very badly put. “long” is indeed an integer in C# I was confused by other previously used languages. Plus, I had to use ulong because this is what the third party code I am using requests.So the multiplying factor as suggested in an answer was indeed the way to go
Strings can store a lot more data than a long. If you convert to a long, you run the risk of not being able to convert it back.
e.g. if I have the string
Now is the time for all good men to come to the aid of their party.that can’t really be converted to a long. “Precision” will be lost.Having said that, A long is a 64 bit integer. It can’t store that kind of data unless you’re willing to change “encoding” somehow. If you have code that looks like this: