1

I want to truncate the value from the given value 32.1500 into 32 and display it on textview text please help me out i searched a lot but did not found anything.

Nidhi Bhatt
  • 71
  • 1
  • 3
  • 12
  • possible duplicate of [Android - Round to 2 decimal places](http://stackoverflow.com/questions/9366280/android-round-to-2-decimal-places) – Apoorv Aug 28 '14 at 09:46
  • sorry the link given for duplicate answers is showing the number with two decimal numbers but i want to truncate the number after decimal. – Nidhi Bhatt Aug 28 '14 at 09:48

3 Answers3

3

If you don't want to cast it you can keep it at double but truncate the trailing zeroes like this:

textView.setText(String.format("%.0f", 5.222));
Simas
  • 43,548
  • 10
  • 88
  • 116
0

Try something like this:

private final DecimalFormat decimalFormat = new DecimalFormat("#.####");
private final DecimalFormat noDecimalFormat = new DecimalFormat("#");

String decimalValue = decimalFormat.format(value);
String noDecimalValue = noDecimalFormat.format(value);

Or using NumberFormat instead of DecimalFormat:

 private NumberFormat formatter;
 // Set the properties for the number format that will display the values
 formatter = NumberFormat.getNumberInstance();
 formatter.setMinimumFractionDigits(4);
 formatter.setMaximumFractionDigits(4);

 String decimalValue = formatter.format(value);

 formatter.setMinimumFractionDigits(0);
 formatter.setMaximumFractionDigits(0);

 String noDecimalValue = formatter.format(value);

You can set the rounding mode you want like this:

noDecimalFormat.setRoundingMode(RoundingMode.DOWN)
formatter.setRoundingMode(RoundingMode.DOWN)
Ionut Negru
  • 6,186
  • 4
  • 48
  • 78
0

Try to use cast like this.

double number=32.1500;
int numbereditted=(int) number;
textView.setText("Number= "+numbereditted);

Or use decimal format.

double number=32.1500;
DecimalFormat df=new DecimalFormat("0");
textView.setText("Number= "+df.format(number));
OmerFaruk
  • 292
  • 2
  • 13
  • Your 2nd suggestion won't work as you expected. It will round the number instead of truncating it. So `32.51` will be converted to `33`. – Simas Aug 28 '14 at 09:57