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");
Related
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) );
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.
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);
İ 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
I want to convert "372551.40" to decimal. But I need to see it after converting this format 372.551,40.
To convert it to decimal, you can use:
decimal decimalValue = 0.0;
decimalValue = decimal.Parse("372551.40");
or
decimal.TryParse("372551.40", out decimalValue);
To display it in a specific format you can do:
CultureInfo tr = new CultureInfo("tr-TR");
string formattedValue = decimalValue.ToString("c", tr);
//result will be 372.551,40 YTL
formattedValue = decimalValue.ToString("0,0.00", tr);
//result will be 372.551,40
string value;
Decimal number;
value = "16,523,421";
if (!Decimal.TryParse(value,out number))
{
// set it to something if the "Value" is not a number
number = -1;
}
Do the following:
string s = "372551.40";
CultureInfo cultureInfo = CultureInfo.InvariantCulure; //Use relevant culture in which your number is formatted. In this case InvariantCulture would do.
decimal d;
bool succesful = Decimal.TryParse(s, NumberStyles.Number, cultureInfo, out d); //it will try to parse the string according to the specified culture.;
If you have a succesful parse, then d will store the numeric value represented by s as a decimal value which you can output into any formatted string and culture the ToString() or Format.String().
Note that if the culture in which the number represented by s is the current system culture, then you can use the TryParse(string s, out decimal d) overload where it is not necessary to specify NumberStyles and IFormatProvider.
Something like this?
string s = "372551.40";
decimal d;
if (decimal.TryParse(s, out d))
{
var culture = new CultureInfo("de-DE");
var result = d.ToString("0,0.00", culture);
// result is "372.551,40"
}
You can also use the current culture instead of hard-coding one like I did.
Hope this helps,
John
Use decimal.Parse() to make it a decimal. Then you have many formatting options.
The display as you mentioned is dependent on the culture setting.
Make your new CultureInfo and in the NumberFormat, you will have to modify some settings like Decimal Separator as , and Thousands Separator as . and provide this to the ToString method of the variable holding the decimal value.
This should display the value as 372.551,40
You can use .Replace
string string 1 = "372,551.40";
string1.Replace(",","");
decimalVal = System.Convert.ToDecimal(StringVal);
//shows 372551.40
You can always throw that into a for loop if you are playign with a ton of numbers.
You can find more in depth info and some examples on MSDN
The overload of decimal.Parse that takes an IFormatProvider will allow you to parse strings containing numbers with periods as decimal point symbols (in case the standard is a comma in your culture).
You can use ToString on the resulting decimal to format it with a comma by passing in an appropriate IFormatProvider. Both CulturInfo and NumberFormatInfo implement IFormatProvider.
You can get an instance of CultureInfo with the following code (this one is for English in Australia).
new CultureInfo("en-AU")
Also note that decimal.TryParse is a good alternative to the decimal.Parse method if you expect incorrectly formatted strings as it will allow you to handle them without an exception being raised.
The following code should give you the desired result (you wrote in one of the comments that the target system is SAP and that the culture is probably German (de-DE)).
var yourString = "372551.40";
var yourDecimal = decimal.Parse(yourString, CultureInfo.InvariantCulture);
var yourFormattedDecimal = yourDecimal.ToString(new CultureInfo("de-DE"));
From MSDN:
string value;
decimal number;
// Parse an integer with thousands separators.
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// 16,523,421' converted to 16523421.
Cheers
You can create custom NumberFormatInfo:
string s = "372551.40";
var dec = decimal.Parse(s, CultureInfo.InvariantCulture);
var nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.NumberGroupSeparator = ".";
nfi.NumberDecimalSeparator = ",";
var res = dec.ToString("n", nfi);
var resDecimal = decimal.Parse(res, nfi);
Output is exactly what you need: 372.551,40