Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a double with two decimals in Android? [duplicate]

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.

like image 284
ArcDare Avatar asked Nov 09 '11 12:11

ArcDare


1 Answers

yourTextView.setText(String.format("Value of a: %.2f", a)); 
like image 147
Goz Avatar answered Sep 29 '22 09:09

Goz