C
C#17mo 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
Down17mo ago
string value: "-3.7193950756378277"
Pobiega
Pobiega17mo ago
Longs can't have decimals Are you thinking of double?
Down
Down17mo ago
okay but tried this with double and didnt work either let me change it back to double
Pobiega
Pobiega17mo ago
Also, decimal parsing is culture specific
WhiteBlackGoose
WhiteBlackGoose17mo ago
$invariantculture
MODiX
MODiX17mo 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
Down17mo ago
Down
Down17mo 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
WhiteBlackGoose17mo ago
I think you should be able to set it ngl it kinda frustrates that C# by default is local
Pobiega
Pobiega17mo ago
yeah you can set currentculture
WhiteBlackGoose
WhiteBlackGoose17mo ago
Apparently some dumbass thought that US-only date format is "invariant culture"
WhiteBlackGoose
WhiteBlackGoose17mo ago
they even did time correctly why date is in random order 🤦
Down
Down17mo ago
bros aint thinkin out there in microsoft
WhiteBlackGoose
WhiteBlackGoose17mo ago
I'm not saying they aren't but some decisions aren't really thought at all
Accord
Accord17mo 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
More Posts
xamarin errori get this error running xamarin android app on windows running same project on macos works❔ Tell DbProviderFactory what connection string to use not working?```DbProviderFactories.RegisterFactory("System.Data.MySql", MySqlConnectorFactory.Instance); service❔ How can i calculate a rotation float from a direction vector?i know this might not be the correct place to ask such a question, but i figured such a large number❔ How can I better understand the concepts of unit of work and repository pattern in C# .NET 7?I am seeking assistance in understanding the unit of work and repository pattern in C#.NET7. While I❔ Help with a unity Inventoryim making an Inventory for my unity game and im getting this CS1061 error, i Know what the error is,❔ Running an awaitable async Task, but forcing it to run on a specific thread (main thread).Hi everyone, I'm doing some work in Unity and am having an issue where some of the code that I want ❔ Help connect VS-Code to Unity and Simplify running programI want to connect Unity and VS code Also ive done it before but i want to simplify how you run the ❔ Issue with unit testing and projects not startingI'm writing up tests for my controller, but I need to grab an access token for authorization to do a❔ Retrieve API data in C#What structure would you use if you want to create an API that retrieves data from another API and r❔ HttpRequestException: No Such Host is known. (url.com:443) using WorkerService issueGreetings! I'm getting this error when my Win Service attempts to connect to my endpoint. It throws