Converting String To Float in C#
Your thread's locale is set to one in which the decimal mark is "," instead of ".".
Try using this:
float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
Note, however, that a float cannot hold that many digits of precision. You would have to use double or Decimal to do so.
Convert a string to float
There are a lot of problems using floats. I tend to use doubles, which does the same thing(?)
When I run:
var inputString = "123.46.789-01";
var strippedString = inputString.Replace(".", "").Replace("-", "");
float floatValue = float.Parse(strippedString);
I get the value: 1,234679E+09
which is an alternative way of displaying 1234678901
.
Confirm by adding this line to the code:
double doubleValue = Convert.ToDouble(floatValue);
and you'll get 1234678901
.
Convert string to double or float c#
I suspect your default culture info uses comma as a decimal separator instead of dot. If you know your input will have a dot, it's best to specify the invariant culture explicitly:
double d1 = double.Parse(s1, CultureInfo.InvariantCulture);
double d2 = double.Parse(s2, CultureInfo.InvariantCulture);
However, if the decimal values matter, you should consider using decimal
instead of either float
or double
:
decimal d1 = decimal.Parse(s1, CultureInfo.InvariantCulture);
decimal d2 = decimal.Parse(s2, CultureInfo.InvariantCulture);
C# String to Float Conversion
You need to pass the CultureInfo for the culture that the strings are formatted in.
http://msdn.microsoft.com/en-us/library/3s27fasw.aspx
The example from MSDN would be:
double number;
string value = "1,097.63";
NumberStyles style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
if (Double.TryParse(value, style, culture, out number))
Console.WriteLine("Converted '{0}' to {1}.", value, number);
else
Console.WriteLine("Unable to convert '{0}'.", value);
Alternatively, if your input strings are formatted differently then use CultureInfo.CurrentCulture
Convert String to Float in ASP.Net Controller
@Sxntk say to me in the comment,
Your decimal separator isn't "." It is a "," That's your local enviroment settings. It should work with "7,8"
Converting string to float issue
If your string uses dot (.) as decimal separator, you should use this overload:
float xFreq = Convert.ToSingle(param, CultureInfo.InvariantCulture);
Not really sure, but Additional information looks Slavic to me and the default decimal separator might be comma (,) instead of dot. Of course, this depends on the culture of the current thread which depends on Regional settings.
Related Topics
High Memory Consumption with Enumerable.Range
Using Bindingoperations.Enablecollectionsynchronization
How to Fill Forms and Submit with Webclient in C#
Retrieve the Respective Coordinates of All Words on the Page with Itextsharp
How to Extract Subscript/Superscript Properly from a PDF Using Itextsharp
The Name '...' Does Not Exist in the Current Context
Convert PDF to Image Without Using Ghostscript Dll
How to Spawn Threads on Different CPU Cores
Display Custom Error Page When File Upload Exceeds Allowed Size in ASP.NET MVC
Providing Input/Subcommands to a Command (Cli) Executed with Ssh.Net Sshclient.Runcommand
How to I Apply Filter While Paginating in ASP.NET MVC and Entity Framework
Recommendation for C# Matrix Library
Testinitialize VS Classinitialize
What Happens in Beginprocessrequest()
C#: Triggering an Event When an Object Is Added to a Queue
Static Property Using Inotifypropertychanged. C#
Excel Error Hresult: 0X800A03Ec While Trying to Get Range with Cell's Name