896+ Capstone Project is Available: Whatsapp: +91-7011258995, Email: sharecodepoint@gmail.com

Get Two digit after decimal point in Android Studio or Java

How to get two digits after the decimal point in java and android studio
private static String getDecimalFormat(double value) {
    String getValue = String.valueOf(value).split("[.]")[1];
      if (getValue.length() == 1) {
          return String.valueOf(value).split("[.]")[0] +
                "."+ getValue.substring(0, 1) + 
                String.format("%0"+1+"d", 0);
       } else {
          return String.valueOf(value).split("[.]")[0]
            +"." + getValue.substring(0, 2);
      }
 }
Output:

System.out.println("value getDecimalFormat:- " + getDecimalFormat(1.0));
value getDecimalFormat:- 1.00

System.out.println("value getDecimalFormat:- " + getDecimalFormat(0.22437891));
value getDecimalFormat:- 0.22

System.out.println("value getDecimalFormat:- " + getDecimalFormat(-54.0));
value getDecimalFormat:- -54.00

System.out.println("value getDecimalFormat:- " + getDecimalFormat(38495.963741));

value getDecimalFormat:- 38495.96

Sharecodepoint

Sharecodepoint is the junction where every essential thing is shared for college students in the well-defined packets of codes. We are focused on providing you the best material package like Question papers, MCQ'S, and one NIGHT STUDY MATERIAL. facebook twitter youtube instagram

Post a Comment

Previous Post Next Post

Contact Form