2

I'm getting the String type decimal number from the local database.

And

I viewed three decimal places in my UI application(html page).so now i need to limit it as 2 deciaml places.

Example String Amount = "123.786";

change the amount decimal value to "123.78"(to be displayed in my web application)

Please help to limit it to two decimal places.

I tried using Stringutils functionality but i can't absorb it.

Yellow Flash
  • 862
  • 2
  • 10
  • 29
  • The DecimalFormat class has methods for specifying the number of digits in a String displaying a number. – NormR Sep 23 '13 at 13:13

1 Answers1

2
 String Amount = "123.786";
 String value= String.format("%.2f", Float.valueOf(Amount));

or 

BigDecimal value = new BigDecimal(Amount).setScale(2, BigDecimal.ROUND_HALF_UP);
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • 1
    it worked!thanks!! please say how to limit the Timestamp value upto seconds.is there any function to cut off milliseconds from timestamp – Yellow Flash Sep 23 '13 at 13:37
  • Happy to hear it.. Please define your question briefly and post another question with input and expected output..All we are waiting helping to you....... – Prabhakaran Ramaswamy Sep 23 '13 at 13:40