This question already has answers here:
Parse strings to double with comma and point
(14 answers)
Closed 8 years ago.
I am trying to read a number from the console, but if it is input via comma (ex. 2,56), it cannot be parsed successfully. It can be parsed only if it is input like this: 2.56
How can I change that?
Here is a sample code:
if (double.TryParse(stringElements[i], out doubleNum))
{
averageTime.Add(doubleNum);
}
replace the comma by '.' like this :
save your number in variable X , then
if (double.TryParse((X).Replace(",", "."), out tmp))
{
}
replace with "."
Convert.ToDouble("2,56".Replace(",", "."));
Related
This question already has answers here:
How can I add double quotes to a string that is inside a variable?
(20 answers)
Escape double quotes in a string
(9 answers)
String.Format store double quotes inside string [duplicate]
(4 answers)
Closed 2 years ago.
I need to pass the folder path to the Python interface but spaces in the path make it complicated.
Example :
Defect.py C:\TestImage\Images works fine.
However, if there is a space in the path as below,
`Defect.py C:\**Test Image**\Images`
causes an error due to two separate parameters.
If I could pass it as :
Defect.py "C:\**Test Image**\Images" should work
So I am currently trying to use string.Format("{0}") to format this string with double quotations but does not work.
Please let me know how I could format the statement using string.Format. I need to use string.Format since there are multiple other parameters.
Try this:
string.Format("Defect.py \"C:\\{0}\\Images\"", "Test Image");
Bear in mind that the whole trick is to escape the character using the \" backslash escape character. The rest of it was just to show how to use the string.Format
This question already has answers here:
C# string splitting
(4 answers)
Closed 7 years ago.
I need to be able to extract the three strings separated by a dashes "-" from an input string.
For example:
Mystring="54-0-9";
the values separated by "-" are with unknown length because they should be an input for the user.
The user should enter each value in a textField than my code concatenate the 3 values and put it as shown . Later i want to get the three values separated again each one in a new text field. How can i do that in c# !?
Using string split.
// Select on "-".
string[] split = _string.Split(new Char[] { '-' });
split[0], split[1], split[2] will have the values you want.
Mystring="54-0-9";
Mystring.Split('-');
this will give you a array of 3 now.
This question already has answers here:
Why does 0.ToString("#.##") return an empty string instead of 0.00 or at least 0?
(5 answers)
Closed 8 years ago.
I have several decimal numbers that I need to show as strings (no need for decimal places). I use the following code:
mytextblock.text= mydecimalnumber.ToString("#");
However this code will show nothing if the number is 0. I need to display a "0" instead of an empty string. I can do this using if and else but I don't think this is the best solution to do with many decimal variables.
Some user "David.." posted this answer and it worked for me but then he deleted it later:
He mentioned that ToString("#") will only show digits that are not zeros.
In order to default to 0, I should use ToString("0")
This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx
This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 9 years ago.
I have a integer like this i.e 3356890. I'm converting it to string and showing on screen.
Now I want to display like this 3,356,890.
How to do?
You can use:
value = 1234567890;
Console.WriteLine(value.ToString("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
However, for the purpose of internationalization and localization, it's probably best to allow the user's current culture settings to determine how to format the number.
Further Reading
Custom Numeric Format Strings
Take a look please here,
Number string format
string res = string.Format(CultureInfo.InvariantCulture, "{0:#,##0}", 3356890);
int val = 3356890;
string valString = val.ToString("#,##0")