Convert ".1", ".2" to double [duplicate] - c#

This question already has answers here:
double.Parse throw a System.FormatException
(3 answers)
Closed 7 years ago.
I want to convert string like ".1", ".2", etc into 0.1, 0.2 etc.
I tried:
Convert.ToDouble(".1")
and it crashes. I understand why it crashes, but I still need to convert it to valid double number. I know I can do this with splitting string and parsing but is there a better way?

Convert.ToDouble uses current culture settings by default.
Probably your current culture has different string than . as a NumberDecimalSeparator.
As a solution, you can use a culture that already has . as a NumberDecimalSeparator like InvariantCulture, or your can Clone your CurrentCulture, set it's NumberDecimalSeparator to . and use that cloned culture in your Convert.ToDouble method (or double.Parse) as a second parameter.
var d = double.Parse(".1", CultureInfo.InvariantCulture);
or
var clone = (CultureInfo)CultureInfo.CurrentCulture.Clone();
clone.NumberFormat.NumberDecimalSeparator = ".";
var d = double.Parse(".1", clone);

Try this:
Double result = Double.Parse(".1", CultureInfo.InvariantCulture);
by specifing CultureInfo.InvariantCulture you ensure . to be a decimal separator.

Related

C#: Cannot Parse String to double [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 4 years ago.
_day.Latitude = "64.128339"
_day.Longitude = "-20.995595"
public override void Prepare(ItineraryDay day)
{
_day = day;
var isParseLatitude = double.TryParse(_day.Latitude, out var latitude);
var isParseLongitude = double.TryParse(_day.Longitude, out var longitude);
_dayPositionInfo = new DayPositionInfo(
latitude,
longitude,
_day.RouteName);
}
As you can see in the picture, Latitude and Longitude are valid double values, but Parse does not work and returns false?
Why, I do not understand something?
I suspect your current culture doesn't use the . character as a decimal separator, that's why Double.TryParse returns false.
If you know that the decimal separator is always a ., you can do something like this:
bool isValidLatitude = Double.TryParse("64.128", NumberStyles.Any, CultureInfo.InvariantCulture, out double latitude);
PS: if you need to check what is the decimal separator of your current culture, use
Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
Convert.ToDouble("64.128339");
double.Parse("64.128339");

C# how to parse a string to double [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 6 years ago.
I want to parse my string to double. My problem is that the result of newTime is 800, but the result should be 8,00.
string time = "08:00";
double newTime = double.Parse(time.Replace(':', '.'));
If you want to treat : as a decimal separator, just do it:
string time = "08:00";
// when parsing "time", decimal separator is ":"
double newTime = double.Parse(time,
new NumberFormatInfo() { NumberDecimalSeparator = ":" });
Try avoiding tricks with magic constants like '.' in the time.Replace(':', '.'). Please, notice that newTime will be 8, not 8.00 (since 8 == 8.0 == 8.00 == 8.000...). If you want to represent newTime with two digits after the decimal point use formatting:
// F2 - format string ensures 2 digits after the decimal point
// Outcome: 8.00
Console.Write(newTime.ToString("F2"));
The result of Double.Parse is a Double, not a string. You need to output a string from the double, using ToString.
You should also use an overload of Double.Parse that has a NumberStyles parameter. Using the Float value allows exponent notation.
string time = "08:00";
double newTime = double.Parse(time.Replace(':', '.'), CultureInfo.InvariantCulture);
Your issue is that your culture has a different decimal separator to what you are creating in your string.
You can change it to to this
string time = "08:00";
double newTime = double.Parse(time.Replace(":", Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator) );

String with negative float to float? [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 8 years ago.
Here is basically the code I have :
string text = "-0.05537987";
float value = Single.Parse(text);
When I try and parse a negative number like I did here I get "Input string was not in the correct format."
Your parsing should work, I believe your current culture has a different character for decimal separator. Use CultureInfo.InvariantCulture
float value = Single.Parse(text, CultureInfo.InvariantCulture);
Your code works fine for me - https://dotnetfiddle.net/JfPfss
Use InvariantCulture and see if that is the issue:
float value = Single.Parse(text, CultureInfo.InvariantCulture);
Just use CultureInfo.InvariantCulture
string text = "-0.05537987";
float value = Single.Parse(text, CultureInfo.InvariantCulture);
MessageBox.Show("your value = "+value);

What is the alternative in C# [duplicate]

This question already has answers here:
Format string by CultureInfo
(5 answers)
Closed 8 years ago.
I found how to change the locale format in Java, but I want to use it in C#.
This is the (Java) line of code
String.format(Locale.US, "%f", floatValue)
For Culture you can Use
CultureInfo en = new CultureInfo("en-US");
and for Formatting Float with Culture you can use
string.Format(new System.Globalization.CultureInfo("en-Us"), "{N2}", floatValue)
C# equivalent is (simple to string conversion)
String result = floatValue.ToString(new CultureInfo("en-US"));
Or (formatting)
String result = String.Format(new CultureInfo("en-US"), "{0}", floatValue);
By default in .Net if you use CultureInfo.InvariantCulture it is associated with the English language en-US
or you can explicitly set CUltureInfo
float value = 16325.62015;
// Display value using the invariant culture.
Console.WriteLine( value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-US culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-US")));

Globalization .net convert string to double

İ have string like this and i want to convert it to double.
string x = "65.50";
double y = Convert.ToDouble(x);
But the result is 6550.0
i want it to be 65.50.
I am using ASP.NET and C#. I think it is a problem about globalization.
This is my for question sorry about that (:
Yes, it's your current culture that converts it this way. You can use CultureInfo.InvariantCulture to skip using your culture.
double d = double.Parse("65.50", CultureInfo.InvariantCulture);
i want it to be 65.50.
If you want to convert it back to string:
string str = d.ToString("N2", CultureInfo.InvariantCulture);
I assume this is a currency since you keep the decimal places. Then you should use decimal instead:
decimal dec = decimal.Parse("65.50", CultureInfo.InvariantCulture); // 65.5
Now you can use decimal.ToString and it automagically restores the decimal places:
string str = dec.ToString(CultureInfo.InvariantCulture); // "65.50"
Recently i had a similar problem. The solution :
var result = Double.TryParse(x, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out y);
if you get true, that's x is converted to double
Try this:
var res = Double.Parse("65.50", NumberStyles.Float, CultureInfo.InvariantCulture);
It will parse it with culture where . is floating separator
Here you can try it: http://ideone.com/3LMVqa

Categories

Resources