سؤال

public static void main(String[] args) {
DecimalFormat df = new DecimalFormat("#.0");

System.out.println(df.format(180.0)); // prints 180.0
System.out.println(df.format(0.0)); // prints .0 BUT I want 0.0
}

How do I change DecimalFormat df = new DecimalFormat("#.0"); constructor parameters so that I can print 0.0???

هل كانت مفيدة؟

المحلول

Try

DecimalFormat df = new DecimalFormat("0.0");
System.out.println(df.format(0.0));

by using

#.0

you omit the leading 0 yourself.

نصائح أخرى

new DecimalFormat("0.0") will do the trick.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top