Format double values ​​using a maximum of five common digits, rounding decimal digits if necessary

I have double values ​​that I would like to convert to String values ​​with the following format restrictions:

number_of_fraction_digits = max (0, 5 - number_of_integer_digits)

Essentially, I want, if possible, to keep the number of digits to 5, rounding decimal digits, if necessary. For instance:

  float String
 -------------------------
      eleven
    100 100
 100000 100000
  99999 99999
  99999.99 99999
   9999.99 9999.9
    999.99 999.99
     23.34324 23.343

I have studied using DecimalFormat , but as far as I can tell, it doesn’t quite do what I need.

It allows you to set the maximum number of decimal digits with setMaximumFractionDigits() , but as far as I can tell, I will have to calculate the number of integer digits and perform the above calculation.

So, the main question is whether there is a beautiful, clean, built-in way to format numbers in this way.

+6
source share
2 answers
 public class SignificantFormat { public static String formatSignificant(double value, int significant) { MathContext mathContext = new MathContext(significant, RoundingMode.DOWN); BigDecimal bigDecimal = new BigDecimal(value, mathContext); return bigDecimal.toPlainString(); } public static void main(String[] args) { double[] data = { 1, 100, 100000, 99999, 99999.99, 9999.99, 999.99, 23.34324 }; for(double d: data){ System.out.printf("Input: %10s \tOutput: %10s\n", Double.toString(d), formatSignificant(d, 5)); } } } 
+6
source

The answer to eee is much cleaner and it gets almost all the way. However, I have a strict requirement that the number of digits (not including the optional decimal point) always be less than a given number (say 5).

This includes any leading 0. for a completely fractional number.

So:

Input: 0.111111 Output: 0.11111

too long and should be:

Input: 0.111111 Output: 0.1111

This approach is much less elegant, but more specific in terms of guaranteeing a finite string length.

I posted it here for consideration, as it may be the final code I came across to solve the problem, although it is less elegant.

 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 ) ); } } 
+2
source

Source: https://habr.com/ru/post/912238/


All Articles