c# - try convert string to decimal -
the code below not work. 1)it not converting in try function double liczbaa=convert.todouble(a) not doing , skipping exception , 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; = 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); } } }
so there 3 issues here:
- properly interpreting text box values double
- respecting culture-specific decimal separator
- returning result decimal separator instead of scientific notation
based on this, following should going. please note usage of double.tryparse() , tostring("f6") formats result floating point number 6 decimals behind 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; = 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); } }
Comments
Post a Comment