Ответ Eee намного чище, и почти до конца. Однако у меня есть строгое требование, чтобы количество цифр (не включая необязательный десятичный знак) всегда было меньше заданного числа (скажем, 5).
Сюда входит любой ведущий 0.
для дробного числа.
Итак:
Input: 0.111111 Output: 0.11111
слишком длинный и должен быть:
Input: 0.111111 Output: 0.1111
Этот подход гораздо менее элегантен, но более конкретен в отношении обеспечения окончательной длины строки.
Я разместил его здесь для рассмотрения, поскольку это может быть окончательный код, который я использую для решения проблемы, даже если он менее элегантен.
public static String format( double value, int totalDigits )
{
String s = String.valueOf( value );
int decimal = s.indexOf( '.' );
// there is no decimal part, so simply return the String
if ( decimal == -1 )
{
return s;
}
else
{
int finalLength;
// example: 23.34324
// the final result will be length totalDigits + 1 because we will include the decimal
if ( decimal < totalDigits )
{
finalLength = totalDigits + 1;
}
// example: 99999
// the final result will be length totalDigits because there will be no decimal
else if ( decimal == totalDigits )
{
finalLength = totalDigits;
}
// example: 999999.999
// we can't make the final length totalDigits because the integer portion is too large
else
{
finalLength = decimal;
}
finalLength = Math.min( s.length( ), finalLength );
return s.substring( 0, finalLength );
}
}
public static void main( String[] args )
{
double[] data = { 1, 100, 1000, 10000, 100000, 99999, 99999.99, 9999.99, 999.99, 23.34324, 0.111111 };
for ( double d : data )
{
System.out.printf( "Input: %10s \tOutput: %10s\n", Double.toString( d ), format( d, 5 ) );
}
}