Jump to content

[C#] How do i convert from a string to double? [Resolved]

By dashker
in Miscellaneous

Recommended Posts

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

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

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

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

 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

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

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 :D i have the idea

Link to comment
Share on other sites

×
×
  • Create New...