public static int formatFloatFast(float value, int maxFractionDigits, byte[] asciiBuffer)
    {
        if (Float.isNaN(value) ||
                Float.isInfinite(value) ||
                value > Long.MAX_VALUE ||
                value <= Long.MIN_VALUE ||
                maxFractionDigits > MAX_FRACTION_DIGITS)
        {
            return -1;
        }

        int offset = 0;
        long integerPart = (long) value;

        //handle sign
        if (value < 0)
        {
            asciiBuffer[offset++] = '-';
            integerPart = -integerPart;
        }
        
        //extract fraction part 
        long fractionPart = (long) ((Math.abs((double)value) - integerPart) * POWER_OF_TENS[maxFractionDigits] + 0.5d);
        
        //Check for rounding to next integer
        if (fractionPart >= POWER_OF_TENS[maxFractionDigits]) {
            integerPart++;
            fractionPart -= POWER_OF_TENS[maxFractionDigits];
        }

        //format integer part
        offset = formatPositiveNumber(integerPart, getExponent(integerPart), false, asciiBuffer, offset);
        
        if (fractionPart > 0 && maxFractionDigits > 0)
        {
            asciiBuffer[offset++] = '.';
            offset = formatPositiveNumber(fractionPart, maxFractionDigits - 1, true, asciiBuffer, offset);
        }

        return offset;
    }
@Test
    void testFormattingInRange()
    {
        //Define a range to test
        BigDecimal minVal = new BigDecimal("-10");
        BigDecimal maxVal = new BigDecimal("10");
        BigDecimal maxDelta = BigDecimal.ZERO;
        
        Pattern pattern = Pattern.compile("^\\-?\\d+(\\.\\d+)?$");
        
        byte[] formatBuffer = new byte[32];
        
        for (int maxFractionDigits = 0; maxFractionDigits <= 5; maxFractionDigits++)
        {
            BigDecimal increment =  new BigDecimal(10).pow(-maxFractionDigits, MathContext.DECIMAL128);
            
            for (BigDecimal value = minVal; value.compareTo(maxVal) < 0; value = value.add(increment))
            {
                //format with the formatFloatFast method and parse back
                int byteCount = NumberFormatUtil.formatFloatFast(value.floatValue(), maxFractionDigits, formatBuffer);
                assertNotEquals(-1, byteCount);
                String newStringResult = new String(formatBuffer, 0, byteCount, StandardCharsets.US_ASCII);
                BigDecimal formattedDecimal = new BigDecimal(newStringResult);
                
                //create new BigDecimal with float representation. This is needed because the float
                //may not represent the 'value' BigDecimal precisely, in which case the formatFloatFast
                //would get a different result.
                BigDecimal expectedDecimal = new BigDecimal(value.floatValue());
                expectedDecimal = expectedDecimal.setScale(maxFractionDigits, RoundingMode.HALF_UP);
                
                BigDecimal diff = formattedDecimal.subtract(expectedDecimal).abs();
                
                assertTrue(pattern.matcher(newStringResult).matches());
                
                //Fail if diff is greater than maxDelta.
                if (diff.compareTo(maxDelta) > 0)
                {
                    fail("Expected: " + expectedDecimal + ", actual: " + newStringResult + ", diff: " + diff);
                }
            }
        }
    }