MathUtils { public static double roundToSignificantDigits( double value, int n ) { if ( isEqual( value, 0.0 ) ) { return 0.0; } final double d = Math.ceil( Math.log10( value < 0.0 ? -value : value ) ); final int power = n - (int) d; final double magnitude = Math.pow( 10.0, power ); final long shifted = Math.round( value * magnitude ); return shifted / magnitude; }  static boolean isValidDouble( Double d ); static double getFloor( double value ); static double getRounded( double value ); static Object getRoundedObject( Object value ); static double roundSignificant( double value ); static double roundToSignificantDigits( double value, int n ); static double roundFraction( double value, int n ); static int getMin( int number, int min ); static int getMax( int number, int max ); static int getWithin( int value, int min, int max ); static boolean isNumeric( String value ); static boolean isNumericLenient( String value ); static boolean isUnitInterval( String value ); static boolean isPercentage( String value ); static boolean isInteger( String value ); static boolean isPositiveInteger( String value ); static boolean isZeroOrPositiveInteger( String value ); static boolean isCoordinate( String value ); static boolean isNegativeInteger( String value ); static boolean isZero( String value ); static boolean isBool( String value ); static boolean isEqual( Double d1, Double d2 ); static boolean isEqual( double d1, double d2 ); static boolean isZero( double value ); static double zeroIfNull( Double value ); static int getRandom(); static Double getMin( double[] array ); static Double getMax( double[] array ); static Double getAverage( List<Double> values ); static Double getSum( List<Double> values ); static Double parseDouble( String value ); static Integer parseInt( String string ); static double getLowBound( double stdDev, double stdDevNo, double average ); static double getHighBound( double stdDev, double stdDevFactor, double average ); static int divideToCeil( int numerator, int denominator ); static int divideToFloor( int numerator, int denominator ); static final Double ZERO; static final String NUMERIC_REGEXP; static final String NUMERIC_LENIENT_REGEXP; }
@Test public void testRoundToSignificantDigits() { assertEquals( 0.1, MathUtils.roundToSignificantDigits( .1357, 1 ), DELTA ); assertEquals( 0.14, MathUtils.roundToSignificantDigits( .1357, 2 ), DELTA ); assertEquals( 0.136, MathUtils.roundToSignificantDigits( .1357, 3 ), DELTA ); assertEquals( 0.1357, MathUtils.roundToSignificantDigits( .1357, 4 ), DELTA ); assertEquals( -0.1, MathUtils.roundToSignificantDigits( -.1357, 1 ), DELTA ); assertEquals( -0.14, MathUtils.roundToSignificantDigits( -.1357, 2 ), DELTA ); assertEquals( -0.136, MathUtils.roundToSignificantDigits( -.1357, 3 ), DELTA ); assertEquals( -0.1357, MathUtils.roundToSignificantDigits( -.1357, 4 ), DELTA ); assertEquals( 0.14, MathUtils.roundToSignificantDigits( .1357, 2 ), DELTA ); assertEquals( 1.4, MathUtils.roundToSignificantDigits( 1.357, 2 ), DELTA ); assertEquals( 14.0, MathUtils.roundToSignificantDigits( 13.57, 2 ), DELTA ); assertEquals( 140.0, MathUtils.roundToSignificantDigits( 135.7, 2 ), DELTA ); assertEquals( -0.14, MathUtils.roundToSignificantDigits( -.1357, 2 ), DELTA ); assertEquals( -1.4, MathUtils.roundToSignificantDigits( -1.357, 2 ), DELTA ); assertEquals( -14.0, MathUtils.roundToSignificantDigits( -13.57, 2 ), DELTA ); assertEquals( -140.0, MathUtils.roundToSignificantDigits( -135.7, 2 ), DELTA ); }