github.com/scaleway/scaleway-cli@v1.11.1/pkg/pricing/utils.go (about)

     1  package pricing
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  
     7  	"github.com/dustin/go-humanize"
     8  )
     9  
    10  var (
    11  	intZero = big.NewInt(0)
    12  	intOne  = big.NewInt(1)
    13  	ratZero = big.NewRat(0, 1)
    14  	ratOne  = big.NewRat(1, 1)
    15  )
    16  
    17  // Returns a new big.Int set to the ceiling of x.
    18  func ratCeil(x *big.Rat) *big.Int {
    19  	z := new(big.Int)
    20  	m := new(big.Int)
    21  	z.DivMod(x.Num(), x.Denom(), m)
    22  	if m.Cmp(intZero) == 1 {
    23  		z.Add(z, intOne)
    24  	}
    25  	return z
    26  }
    27  
    28  // Returns a new big.Rat set to maximum of x and y
    29  func ratMax(x, y *big.Rat) *big.Rat {
    30  	if x.Cmp(y) < 1 {
    31  		return y
    32  	}
    33  	return x
    34  }
    35  
    36  // Returns a new big.Rat set to minimum of x and y
    37  func ratMin(x, y *big.Rat) *big.Rat {
    38  	if x.Cmp(y) > 0 {
    39  		return y
    40  	}
    41  	return x
    42  }
    43  
    44  // PriceString returns a human reprensetation of a price with a currency
    45  func PriceString(price *big.Rat, currency string) string {
    46  	floatVal, _ := price.Float64()
    47  	return fmt.Sprintf("%s %s", humanize.Ftoa(floatVal), currency)
    48  }