protected function formatIntegerGroupings(
        $value,
        SwatI18NNumberFormat $format
    ) {
        // group integer part with thousands separators
        $grouping_values = array();
        $groupings = $format->grouping;
        $grouping_total = intval(floor(abs($value)));
        if (
            count($groupings) === 0 ||
            $grouping_total === 0 ||
            $format->thousands_separator == ''
        ) {
            array_push($grouping_values, $grouping_total);
        } else {
            $grouping_previous = 0;
            while (count($groupings) > 1 && $grouping_total > 0) {
                $grouping = array_shift($groupings);

                if ($grouping === 0) {
                    // a grouping of 0 means use previous grouping
                    $grouping = $grouping_previous;
                } elseif ($grouping === CHAR_MAX) {
                    // a grouping of CHAR_MAX means no more grouping
                    array_push($grouping_values, $grouping_total);
                    break;
                } else {
                    $grouping_previous = $grouping;
                }

                $grouping_value = floor(
                    fmod($grouping_total, pow(10, $grouping))
                );

                $grouping_total = floor($grouping_total / pow(10, $grouping));
                if ($grouping_total > 0) {
                    $grouping_value = str_pad(
                        $grouping_value,
                        $grouping,
                        '0',
                        STR_PAD_LEFT
                    );
                }

                array_push($grouping_values, $grouping_value);
            }

            // last grouping repeats until integer part is finished
            $grouping = array_shift($groupings);

            // a grouping of CHAR_MAX means no more grouping
            if ($grouping === CHAR_MAX) {
                array_push($grouping_values, $grouping_total);
            } else {
                // a grouping of 0 means use previous grouping
                if ($grouping === 0) {
                    $grouping = $grouping_previous;
                }

                // a grouping of 0 as the last grouping means no more grouping
                if ($grouping === 0) {
                    array_push($grouping_values, $grouping_total);
                } else {
                    while ($grouping_total > 0) {
                        $grouping_value = floor(
                            fmod($grouping_total, pow(10, $grouping))
                        );

                        $grouping_total = floor(
                            $grouping_total / pow(10, $grouping)
                        );

                        if ($grouping_total > 0) {
                            $grouping_value = str_pad(
                                $grouping_value,
                                $grouping,
                                '0',
                                STR_PAD_LEFT
                            );
                        }

                        array_push($grouping_values, $grouping_value);
                    }
                }
            }
        }

        $grouping_values = array_reverse($grouping_values);

        // join groupings using thousands separator
        $formatted_value = implode(
            $format->thousands_separator,
            $grouping_values
        );

        return $formatted_value;
    }