119

Maybe this is a silly question, but I cannot guess how to solve it if it's not creating a method. Maybe there's a "natural way" to do it, like in C for example. Here's the problem:

I have a var:

double a;

And I want to show it only with 2 or 3 decimals. When I try to show it:

Text.setText("Value of a: " + String.valueOf(a));

It gives something like:

Value of a: 5.234966145

And i would want just

Value of a: 5.23

Without changing the real value of a so it shows the approximate number but works with the real number.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382
ArcDare
  • 3,106
  • 4
  • 27
  • 38
  • 2
    doubleNumber.toString().substring(0, 3) – Madi Feb 01 '18 at 19:37
  • 1
    input?.let { if (input == "0" || input == "0.0") { return "0.00" } return try { val formatter = DecimalFormat("#,###,###.00") formatter.format(input.toDouble()) } catch (e: Exception) { it } } – Atif AbbAsi Sep 12 '21 at 19:38

5 Answers5

230
yourTextView.setText(String.format("Value of a: %.2f", a));
Louis CAD
  • 10,965
  • 2
  • 39
  • 58
Goz
  • 61,365
  • 24
  • 124
  • 204
  • 1
    This gives a warning: "Implicitly using the default locale is a common source of bugs: Use String.format(Locale, ...) instead" – AndreKR Jul 15 '20 at 11:22
  • @AndreKR that's because of variable symbols used in different countries. Even if you specify the correct formatting, you have to do the same if you parse it later. If you aren't doing that, it doesn't matter AT ALL because it uses the system settings of the device, which you should use anyway. – John Lord Feb 04 '21 at 17:59
53

For Displaying digit upto two decimal places there are two possibilities - 1) Firstly, you only want to display decimal digits if it's there. For example - i) 12.10 to be displayed as 12.1, ii) 12.00 to be displayed as 12. Then use-

DecimalFormat formater = new DecimalFormat("#.##"); 

2) Secondly, you want to display decimal digits irrespective of decimal present For example -i) 12.10 to be displayed as 12.10. ii) 12 to be displayed as 12.00.Then use-

DecimalFormat formater = new DecimalFormat("0.00"); 
Fenil
  • 1,194
  • 10
  • 11
47

You can use a DecimalFormat, or String.format("%.2f", a);

JJD
  • 50,076
  • 60
  • 203
  • 339
FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
36

Before you use DecimalFormat you need to use the following import or your code will not work:

import java.text.DecimalFormat;

The code for formatting is:

DecimalFormat precision = new DecimalFormat("0.00"); 
// dblVariable is a number variable and not a String in this case
txtTextField.setText(precision.format(dblVariable));
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
M. A. Hatab
  • 361
  • 3
  • 3
8
textView2.setText(String.format("%.2f", result));

and

DecimalFormat form = new DecimalFormat("0.00");
         textView2.setText(form.format(result) );

...cause "NumberFormatException" error in locale for Europe because it sets result as comma instead of point decimal - error occurs when textView is added to number in editText. Both solutions are working excellent in locale US and UK.

Ranko1977
  • 91
  • 1
  • 4