C
C#2y ago
Down

❔ error when converting string to long

System.FormatException: Input string was not in a correct format.
System.FormatException: Input string was not in a correct format.
you can see on image from what im trying to convert
public string ChangePercent24Hr { get; set; }
public long ChangePercent24HrD => long.Parse(ChangePercent24Hr);
public string ChangePercent24Hr { get; set; }
public long ChangePercent24HrD => long.Parse(ChangePercent24Hr);
15 Replies
Down
DownOP2y ago
string value: "-3.7193950756378277"
Pobiega
Pobiega2y ago
Longs can't have decimals Are you thinking of double?
Down
DownOP2y ago
okay but tried this with double and didnt work either let me change it back to double
Pobiega
Pobiega2y ago
Also, decimal parsing is culture specific
WhiteBlackGoose
$invariantculture
MODiX
MODiX2y ago
In different cultures "," and "." are used for decimal points (and even other symbols sometimes). Here what you can write use Invariant culture (with dot):
float.Parse("1.2", CultureInfo.InvariantCulture)
float.Parse("1.2", CultureInfo.InvariantCulture)
Or use Try-pattern
using System.Globalization;
if (float.TryParse("1.2", NumberStyles.Any, CultureInfo.InvariantCulture, out var res))
Console.WriteLine($"Parsed! {res}");
else
Console.WriteLine("Failed");
using System.Globalization;
if (float.TryParse("1.2", NumberStyles.Any, CultureInfo.InvariantCulture, out var res))
Console.WriteLine($"Parsed! {res}");
else
Console.WriteLine("Failed");
Down
DownOP2y ago
Down
DownOP2y ago
let me add Culture works now impossible thanks can i set like global Culture or i need to add this argument to every Parse method
WhiteBlackGoose
I think you should be able to set it ngl it kinda frustrates that C# by default is local
Pobiega
Pobiega2y ago
yeah you can set currentculture
WhiteBlackGoose
Apparently some dumbass thought that US-only date format is "invariant culture"
WhiteBlackGoose
they even did time correctly why date is in random order 🤦
Down
DownOP2y ago
bros aint thinkin out there in microsoft
WhiteBlackGoose
I'm not saying they aren't but some decisions aren't really thought at all
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server