java - simple android calculator returning too many zeros and wrong decimal point -
so output "h" in gives me long of number decimal point in wrong spot, otherwise whole number correct. example:
333433.33333
gets displayed 333.43
should displayed
i suspect culprint
`h = (double) math.round(h * 100000) / 100000;`
but when change h = (double) math.round(h * 1000) / 1000;
doesn't seem help.
public class dofcalculator extends fragment { edittext txtf; spinner aspinner, cspinner; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.calculators_fragment_dof, container, false); button button = (button) rootview.findviewbyid(r.id.btn_cal); aspinner = (spinner) rootview.findviewbyid(r.id.aspinner); txtf = (edittext) rootview.findviewbyid(r.id.focal_length); cspinner = (spinner) rootview.findviewbyid(r.id.coc); final textview txtanswer = (textview) rootview.findviewbyid(r.id.txt_h); button.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { string f = txtf.gettext().tostring(); string = aspinner.getselecteditem().tostring(); string c = cspinner.getselecteditem().tostring(); if (!f.isempty() ) { txtanswer.settext("h = " + calcullate_h(f, a, c) + ""); } else { toast.maketext(getactivity(), "all data required", toast.length_long).show(); } } }); return rootview; } private double calcullate_h(string txtf, string txta, string txtc) { double f = double.parsedouble(txtf.tostring()); double = double.parsedouble(txta.tostring()); double c = double.parsedouble(txtc.tostring()); double h = ((f * f) / (a * c)) + f; h = (double) math.round(h * 100000) / 100000; return h; } }
from understand, h
off factor of 1000, , textview
showing many decimal points. recommend:
1) divide h
1000 in calculate_h
. (i.e., return h / 1000
)
2) use string.format
show desired number of digits after decimal point. example, txtanswer.settext(string.format("h = %.2f", calculate_h(f, a, c)))
show 2 digits after decimal point.
Comments
Post a Comment