class 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;
    }

}