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);
Related
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");
I'm trying to convert a double to a decimal with a dot instead of a comma. I feel like I have tried every single possible way (except a working one) so I'm out of ideas.
double amount = myUsd / price;
string amountAsString = amount.ToString();
decimal value = Decimal.Parse(amountAsString, CultureInfo.InvariantCulture);
This one gives a FormatException for example.
Thanks
As Hans Passant said, if you Parse in the same way as you Format you'll avoid a lot of issues -> Use string amountAsString = amount.ToString(CultureInfo.InvariantCulture);
EDIT
Since you're passing in an exponential notation you need to tell the Parser this. This worked with your example value:
decimal value = Decimal.Parse(amountAsString, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture);
But I suggest NOT formatting it as a Double. First convert it to a Decimal. Then parse it as a Decimal:
double amount = myUsd / price;
Decimal decAmount = (Decimal)amount;
string amountAsString = decAmount.ToString(CultureInfo.InvariantCulture);
decimal value = Decimal.Parse(amountAsString, CultureInfo.InvariantCulture);
Then it'll never get formatted in exponential notation.
Also see this C# Fiddle snippet
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) );
double value = double.Parse("4655.927411110702", CultureInfo.InvariantCulture);
Why this parsing is getting me result: 4655.9274111107025?
Somehow it adds to my number 5 at the end. How should I convert this string to double and have a correct result?
You can't. Not all numbers can be represented exactly in double precision floating point, and the closest such double to 4655.927411110702 is 4655.927411110702450969256460666656494140625, and the default formatting you're using trims off the majority of the "joke" digits.
C# does have a decimal type. Can you not use that?
decimal value = decimal.Parse("4655.927411110702", CultureInfo.InvariantCulture);
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.