// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package utils

import (
	
	
	
	

	
	
	
)

// FormatDecimalG formats n, a value of type typ, in 'g' style. It returns an
// error only when typ is a Decimal128 or Decimal256 type whose scale is outside
// [-MaxScale, MaxScale]. At absolute scales above 1000, it rounds the exact
// coefficient and shifts the exponent, avoiding conversion work quadratic in
// the scale. Exact ties round to even.
func [ interface{ (int32) *big.Float }]( ,  arrow.DecimalType) (string, error) {
	,  := .GetPrecision(), .GetScale()
	var  int32
	switch .ID() {
	case arrow.DECIMAL128:
		 = decimal128.MaxScale
	case arrow.DECIMAL256:
		 = decimal256.MaxScale
	}
	if  > 0 && ( < - ||  > ) {
		return "", fmt.Errorf("scale %d out of range for decimal%d, must be between %d and %d", , .BitWidth(), -, )
	}

	if  >= -1000 &&  <= 1000 {
		return .().Text('g', int()), nil
	}
	 := .(0)
	if .Sign() == 0 {
		return .Text('g', int()), nil
	}
	// No decimal type has more than 77 coefficient digits, and a negative
	// precision keeps 'g's shortest formatting.
	 := min(int(), 77)
	if  > 0 {
		--
	}
	, ,  := strings.Cut(.Text('e', ), "e")
	,  := strconv.ParseInt(, 10, 64)
	return fmt.Sprintf("%se%+03d", strings.TrimRight(strings.TrimRight(, "0"), "."), -int64()), nil
}