private void appendSignificantNumber(StringBuffer buffer, double number, int pSignificantDecimals, boolean includeTrailingZeros) {
        int significantDecimals = pSignificantDecimals;
        double iPart = Math.floor(number);

        stringBufferAppend(buffer, iPart, 10, true, 0);

        double fPart = MathLib.roundToSignificantNumbers(number - iPart, significantDecimals);

        boolean hasNotStarted = iPart == 0 && fPart != 0;
        int zeroPaddingBefore = 0;
        int scanLength = significantDecimals;
        for (int i = 0; i < scanLength; i++) {
            fPart *= 10.0;
            if (Math.floor(fPart) == 0 && fPart != 0) {
                zeroPaddingBefore++;
                if (hasNotStarted) {
                    scanLength++;
                }
            }
        }
        fPart = MathLib.round(fPart);

        if (!includeTrailingZeros) {
            while (fPart > 0 && (fPart % 10) == 0) {
                fPart /= 10;
                significantDecimals--;
            }
        }

        buffer.append('.');
        int periodPos = buffer.length();
        extend(buffer, zeroPaddingBefore, '0');
        int prePos = buffer.length();
        stringBufferAppend(buffer, fPart, 10, false, 0);
        int postPos = buffer.length();

        int len = postPos - prePos;
        if (includeTrailingZeros && len < significantDecimals) {
            int padRightSize = significantDecimals - len - zeroPaddingBefore;
            extend(buffer, padRightSize, '0');
        }

        if (!includeTrailingZeros && periodPos == buffer.length()) {
            buffer.delete(periodPos - 1, buffer.length());
        }
    }
Only works with non-negative numbers
main/src/main/java/cgeo/geocaching/wherigo/kahlua/stdlib/StringLib.java