I only have access to a function like this (mean I cannot change the input or signature for this function):
public void log( String fmt , final Object... args )
{
fmt = fmt.replace( "%f" , "%1.3e" );
System.out.print( String.format( Locale.US , fmt , args );
}
I'd like to change the fmt to contain either %5.2f or %1.3e (only examples) dependent on the actual decimal value it represents (to kind of achieve the same number of digits for every decimal value).
Lets say 6 digits + 1 dot:
1234.56789 becomes 1234.56
123.456789 becomes 123.456
12.3456789 becomes 12.3456
...
0.00123456789 becomes 1.234e-3
0.000123456789 becomes 1.234e-4
...
How would someone do that?
By "same number of digits", I assume you mean "same number of characters", which is a common requirement for logging functions.
For 6 digits + 1 dots, the following code will output 7 characters for each number :
The ouput being :
In any case, the
07part in the placeholders is the most important, because it guarantees the minimal width of the ouput. The leading0flag ensures that the output is padded with zeroes, but you can take it out if you want spaces instead.