Reputation: 1
The code below does not work. 1)It is not converting in try function double liczbaa=Convert.ToDouble(a) but it does not doing it and its skipping to exception and then program breaks. 1)It doesnt show format exception "bad values" in trzyde_wynik.Text
namespace Kubik
{
public sealed partial class TrzyDe : Page
{
public TrzyDe()
{
this.InitializeComponent();
}
private void wylicz_Click(object sender, RoutedEventArgs e)
{
string a, b, c;
a = wpis_a.ToString();
b = wpis_b.ToString();
c = wpis_c.ToString();
try
{
double liczba1 = Convert.ToDouble(a);
double liczba2 = Convert.ToDouble(b);
double liczba3 = Convert.ToDouble(c);
}
catch(FormatException)
{
trzyde_wynik.Text = "bad values";
}
double liczbaa = Convert.ToDouble(a);
double liczbab = Convert.ToDouble(b);
double liczbac = Convert.ToDouble(c);
double trzyde_w = (liczbaa * liczbab * liczbac) / 1000000;
trzyde_wynik.Text = Convert.ToString(trzyde_w);
}
}
}
Upvotes: 0
Views: 1591
Reputation: 2917
So there are obviously three issues here:
Based on this, the following should get you going. Please note the usage of double.TryParse() and ToString("F6") which formats the result as floating point number with 6 decimals behind the separator:
private void wylicz_Click(object sender, RoutedEventArgs e)
{
string a, b, c;
var culture = System.Globalization.CultureInfo.CreateSpecificCulture("pl-PL");
var style = System.Globalization.NumberStyles.Number;
a = wpis_a.Text;
b = wpis_b.Text;
c = wpis_c.Text;
double liczba1 = 0.0;
double liczba2 = 0.0;
double liczba3 = 0.0;
if (!(double.TryParse(a, style, culture, out liczba1) && double.TryParse(b, style, culture, out liczba2) && double.TryParse(c, style, culture, out liczba3)))
{
trzyde_wynik.Text = "bad values";
}
else
{
double trzyde_w = (liczba1 * liczba2 * liczba3) / 1000000;
trzyde_wynik.Text = trzyde_w.ToString("F6", culture);
}
}
Upvotes: 2