1

I want to do some calculations on my app the result will mostly have from 5 to 15 digits after the . for example 24.61835496354822 I want to display the result in a TextView and only show 2 digits after . for example 24.61 please help me

smaika
  • 65
  • 10
  • go to this [http://stackoverflow.com/questions/9820149/double-parameter-with-2-digits-after-dot-in-strings-xml?rq=1](http://stackoverflow.com/questions/9820149/double-parameter-with-2-digits-after-dot-in-strings-xml?rq=1) – M D Mar 21 '14 at 06:39

4 Answers4

3
double d = 24.61835496354822;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
0

Either use System.out.printf("%.2f", val);` or

DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));

new DecimalFormat("##.##").format(number);
Achrome
  • 7,773
  • 14
  • 36
  • 45
Shriram
  • 4,343
  • 8
  • 37
  • 64
0

Try This :

import java.text.*;  
 class Decimals {  
  public static void main(String[] args) {  
    float f = 24.61835496354822f;  
    DecimalFormat form = new DecimalFormat("0.00");  
    System.out.println(form.format(f));  
  }  
}  
Benjamin
  • 2,257
  • 1
  • 15
  • 24
0

You could use a simple string format like:

String.format("My value is: %.2f",  myFpVal));

If you just want the value, you can make your format string contain just the format instruction like:

String.format("%.2f",  myFpVal));
scottt
  • 8,301
  • 1
  • 31
  • 41