javahtmlstringapache-stringutilsstring-utils

Limitation of Decimal places of String datatype from database to HTML page


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.


Solution

  •  String Amount = "123.786";
     String value= String.format("%.2f", Float.valueOf(Amount));
    
    or 
    
    BigDecimal value = new BigDecimal(Amount).setScale(2, BigDecimal.ROUND_HALF_UP);