dashker Posted June 1, 2016 Share Posted June 1, 2016 Like i said from example in TextBox1.text I have 2.0 How i can got this value? Link to comment Share on other sites More sharing options...
noc Posted June 1, 2016 Share Posted June 1, 2016 like this ? Int_Seconds = int.Parse(TextBox1.Text); Link to comment Share on other sites More sharing options...
Kaev Posted June 1, 2016 Share Posted June 1, 2016 5 minutes ago, noc said: like this ? Int_Seconds = int.Parse(TextBox1.Text); 1. This is int, not double. 2. Just double.Parse wouldn't work, because it actually wants the number as 2,0 instead of 2.0 You can use this: double.Parse("2.0", System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo); Also, don't forget to check it is really something like 2.0. A user could enter 2.a or something and your program will probably crash. Link to comment Share on other sites More sharing options...
noc Posted June 1, 2016 Share Posted June 1, 2016 Thank Kaev, you're right. I found some informations here : http://stackoverflow.com/questions/1354924/how-do-i-parse-a-string-with-a-decimal-point-to-a-double and here : http://stackoverflow.com/questions/4352849/how-to-parse-a-string-to-double Link to comment Share on other sites More sharing options...
dashker Posted June 1, 2016 Author Share Posted June 1, 2016 So i will show you the problem, i am parsing a double, converting to string bubt with the double format double text = double.Parse(ScaleBox.Text, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo); string result = number[0] + "," + number[1]+ "," + number[2] + "," + number[3] + "," + text; Link to comment Share on other sites More sharing options...
Kaev Posted June 1, 2016 Share Posted June 1, 2016 50 minutes ago, dashker said: So i will show you the problem, i am parsing a double, converting to string bubt with the double format double text = double.Parse(ScaleBox.Text, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo); string result = number[0] + "," + number[1]+ "," + number[2] + "," + number[3] + "," + text; Does it work as you wanted or not? You just posted a snippet without any informations. Also, just for future coding style: If number[] is a array of int or double or smth like that, make sure to write number[0].ToString(). Link to comment Share on other sites More sharing options...
dashker Posted June 1, 2016 Author Share Posted June 1, 2016 2 hours ago, Kaev1695989297 said: Does it work as you wanted or not? You just posted a snippet without any informations. Also, just for future coding style: If number[] is a array of int or double or smth like that, make sure to write number[0].ToString(). Tes i have it hahaha but when i put it in a text box for example the "2.0" is 2 so i don't know if i have to add it into the string or another thing Link to comment Share on other sites More sharing options...
dashker Posted June 1, 2016 Author Share Posted June 1, 2016 int[] number = new int[15]; number[0] = int.Parse(EntryBox.Text); number[1] = int.Parse(ModelBox.Text); number[2] = int.Parse(SoundBox.Text); number[3] = int.Parse(DisplayInfoExtraBox.Text); double scale = double.Parse(ScaleBox.Text, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.NumberFormatInfo.InvariantInfo); string result = number[0] + "," + number[1]+ "," + number[2] + "," + number[3] + "," + scale; ShitBox.Text = result; So i don't know how to handle the double so my head is exploding Link to comment Share on other sites More sharing options...
dashker Posted June 2, 2016 Author Share Posted June 2, 2016 Anyone Help me please? Thanks for answers Link to comment Share on other sites More sharing options...
Skarn Posted June 2, 2016 Share Posted June 2, 2016 Isn't there a way to make a TextField return a double or a float without any conversion? Link to comment Share on other sites More sharing options...
barncastle Posted June 2, 2016 Share Posted June 2, 2016 Everything that you're doing is correct however the reason you're missing the trailing zero is because the compiler is rightly removing it as it is irrelevant. 2.0 is exactly the same as 2. If you want to keep them you need to use a string formatter on the double such as scale.ToString("0.00"). I'm on a phone at the moment so can't provide a decent example but look up string formatters in Google. It might also be worth looking at the Microsoft C# video tutorials as they're really good and cover this sort of thing. On a side note; if this is a production project I'd suggest looking up the TryParse method as well as it allows you to handle bad user input. Link to comment Share on other sites More sharing options...
dashker Posted June 2, 2016 Author Share Posted June 2, 2016 27 minutes ago, barncastle said: Everything that you're doing is correct however the reason you're missing the trailing zero is because the compiler is rightly removing it as it is irrelevant. 2.0 is exactly the same as 2. If you want to keep them you need to use a string formatter on the double such as scale.ToString("0.00"). I'm on a phone at the moment so can't provide a decent example but look up string formatters in Google. It might also be worth looking at the Microsoft C# video tutorials as they're really good and cover this sort of thing. On a side note; if this is a production project I'd suggest looking up the TryParse method as well as it allows you to handle bad user input. thanks i have the idea Link to comment Share on other sites More sharing options...
[C#] How do i convert from a string to double? [Resolved]
By dashkerin Miscellaneous
Recommended Posts