Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

duplicate format flag exception

Tags:

java

format

I am getting "duplicate format flag exception" on 4th line. Am I missing something in format?

String fmt = "%1$00.3f";
Object[] obj = new Object[1];
obj[0] = new Double((double) 2);
String.format(fmt,obj) 
like image 816
sattu Avatar asked Jul 30 '13 00:07

sattu


Video Answer


1 Answers

Problem is that you're repeating the flag 0, if you want a float padded with zeros to have at least two digits in the integer part, try this:

String fmt = "%1$06.3f";

The length field (6) means: 3 digits for the fractional part + 1 digit for the dot + 2 digits for the integral part.

like image 67
morgano Avatar answered Sep 27 '22 18:09

morgano