2

So my calculator gives me answer like 30.122345353 or something like that.

Question How can i round it to 30.122 ? Here is part of my code. I need it to round the answer what comes from (ans + "")

This is my code

if (v.getId() == R.id.Badd) if (num2 == 0) flag = true;
    else ans = 2 * num1;
    tv2.setText(ans + "");
Ethaan
  • 11,291
  • 5
  • 35
  • 45
StenK
  • 21
  • 1

5 Answers5

2

easy way for rounding double to 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 something easy:

public Double roundMyValue(String value){
double current=Double.parseDouble(value);
int temp = current*1000;
return (Double)temp/1000;
}
Leonid Veremchuk
  • 1,952
  • 15
  • 27
1

You can use DecimalFormat like this -

DecimalFormat decimalFormat = new DecimalFormat("#0.###");
decimalFormat.format(ans);
KamaL
  • 37
  • 8
1

You can use a DecimalFormater :)

DecimalFormat df=new DecimalFormat("0.000");
String formate = df.format(ans); 
double finalValue = (Double)df.parse(formate) ;

That should solve your problem. But if you want additional help, this question was already answered in a different post on StackOverflow: Here

All the best :)

Please let me know of the outcome.

Good luck!

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
  • Thank you, but exactly where should i enter that code ? – StenK Apr 14 '15 at 21:19
  • Hi there, there are 2 ways to do it. You can either create a function to format any number by 3 decimal places, or you can place the code that I have shown you just above your "tv2.setText" statement. Good luck :) Let me know if theres anything else you require – TejjD Apr 15 '15 at 06:31
  • If i just copy your code and do nothing else, then it doesnt round anything. – StenK Apr 15 '15 at 20:21
1

This solution provides a way to round numbers to a certain number of decimal places

Community
  • 1
  • 1
Thognar
  • 31
  • 1
1

You can beside the other Answers also round the number by multiplying the result times 10^n where n stands for the last significant decimalpoint you wanna achieve then round this(adding 0.5 for proper rounding like mathematicians do)by casting to an int and then divide it by 10^3 back again

maybe a method like this would do the trick:

Java:

public double rndMyNmb(double number,int decimalPoint);
{
    number*=Math.pow(10,decimalPoint);
    number=(int)(number+0.5);
    return number/Math.pow(10,decimalPoint);
}

ofc you could write everything inside the method as one-liner:

return (double)((int)(number*Math.pow(10,decimalPoint)+0.5)/Math.pow(10,decimalPoint));
Ilja KO
  • 1,272
  • 12
  • 26
  • Thank you, but i really dont understand where i have to write that code :/ im new at this – StenK Apr 14 '15 at 21:35
  • lets say the value where your number is stored is called : myNumber – Ilja KO Apr 14 '15 at 21:39
  • then you can round this number like this with the method i provided you: `myNumber=rndMyNmb(myNumber,n); ` //for n just writze the number of decimals you want to get back....thats all the magic then :) – Ilja KO Apr 14 '15 at 21:41