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) );
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");
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:
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 have many decimals, each rounded differently:
decimal quantity = Decimal.Round(item.Quantity.Value, 2,
MidpointRounding.AwayFromZero);
decimal difference = Decimal.Round(quantity * eva, 0,
MidpointRounding.AwayFromZero);
When binding to the UI, I convert to string like this:
string Quantity = quantity.ToString("G", CultureInfo.InvariantCulture);
string Difference = difference.ToString("G", CultureInfo.InvariantCulture);
Is there a generic way to insert commas for thousand separators while keeping the original decimal rounding the same?
Try using Format.
double d = 1.234567;
String output = d.ToString("#,##0.##");
Also,
double d = 123456789.1;
string format = d.ToString().
IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
>=0 ? "#,##0.00" : "#,##0";
Console.WriteLine (d.ToString(format));
For anyone wondering, I ended up using String.Format(new CultureInfo("en-US"), "{0:N}", difference) and changed the N depending on how many decimal places I needed.
You can use the "N" format specifier and supply the number of digits you want any number to retain. If you want each number to potentially have a different number of digits you wall have to determine the number to supply to the format string each time.
quantity.ToString("N(digits)");
Complete documentation is at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString