java - Android calculator number rounding -


this question has answer here:

so calculator gives me answer 30.122345353 or that.

question how can round 30.122 ? here part of code. need round answer comes (ans + "")

this code

if (v.getid() == r.id.badd) if (num2 == 0) flag = true;     else ans = 2 * num1;     tv2.settext(ans + ""); 

easy way rounding double int:

int result = ((int)(yourdouble+0.5)); 

or:

public double roundmyvalue(string value){ decimalformat df=new decimalformat("0.000"); return (double)df.parse(df.format(value)) ; } 

or easy:

public double roundmyvalue(string value){ double current=double.parsedouble(value); int temp = current*1000; return (double)temp/1000; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -