Documentation
¶
Index ¶
- func Abs(value float64) float64
- func Average[T constraints.Integer | constraints.Float](ns ...T) float64
- func BigFloatMul(multiplicand, multiplier *big.Float) *big.Float
- func Ceil(value float64) float64
- func Clamp(value, min, max float64) float64
- func CleanFloat(value float64) float64
- func CleanFloatString(value float64) string
- func CompoundInterest(principal, rate float64, periods int) float64
- func DecimalMax(ds ...decimal.Decimal) decimal.Decimal
- func DecimalMin(ds ...decimal.Decimal) decimal.Decimal
- func DecimalSum(ds ...decimal.Decimal) decimal.Decimal
- func Floor(value float64) float64
- func FormatCurrency(amount float64, decimalPlaces int32) string
- func FormatMoney(amount float64, decimalPlaces int32) string
- func FormatMoneyInt(amount int64, decimalPlaces int32) string
- func Int64Div(dividend, divisor int64, precision int32) float64
- func Int64DivTrunc(dividend, divisor int64, precision int32) float64
- func Int64MulFloat64(multiplicand int64, multiplier float64) float64
- func IsEqual(a, b float64) bool
- func IsNegative(value float64) bool
- func IsPositive(value float64) bool
- func IsZero(value float64) bool
- func Lerp(a, b, t float64) float64
- func Max[T constraints.Ordered](ns ...T) T
- func Median[T constraints.Integer | constraints.Float](ns ...T) float64
- func Min[T constraints.Ordered](ns ...T) T
- func ParseFloat(s string) (float64, error)
- func Percentage(value, percent float64) float64
- func Pow(base, exponent float64) float64
- func RemoveTrailingZeros(value float64) string
- func RemoveTrailingZerosFixed(value float64, places int32) string
- func SafeDiv(dividend, divisor float64, precision int32) float64
- func Sign(value float64) int
- func Sqrt(value float64) float64
- func StandardDeviation[T constraints.Integer | constraints.Float](ns ...T) float64
- func Sum[T constraints.Integer | constraints.Float](ns ...T) T
- func ToFixed(value float64, places int32) float64
- func ToString(value float64) string
- func ToStringBank(value float64, places int32) string
- func ToStringFixed(value float64, places int32) string
- type Result
- func Add(a, b float64) Result
- func Div(a, b float64, precision int32) Result
- func DivTrunc(a, b float64, precision int32) Result
- func Mul(a, b float64) Result
- func NewResult(value float64) Result
- func Round(value float64, precision int32) Result
- func Sub(a, b float64) Result
- func Truncate(value float64, precision int32) Result
- func (r Result) Abs() Result
- func (r Result) Add(other float64) Result
- func (r Result) Clean() Result
- func (r Result) Div(other float64, precision int32) Result
- func (r Result) DivTrunc(other float64, precision int32) Result
- func (r Result) Float64() float64
- func (r Result) FormatMoney(decimalPlaces int32) string
- func (r Result) Mul(other float64) Result
- func (r Result) Neg() Result
- func (r Result) Round(places int32) Result
- func (r Result) String() string
- func (r Result) Sub(other float64) Result
- func (r Result) ToString() string
- func (r Result) ToStringBank(places int32) string
- func (r Result) ToStringFixed(places int32) string
- func (r Result) Truncate(places int32) Result
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abs ¶
Abs returns the absolute value of a number
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Mathematical functions fmt.Printf("Abs(-3.14): %.2f\n", mathx.Abs(-3.14)) fmt.Printf("Ceil(3.14): %.0f\n", mathx.Ceil(3.14)) fmt.Printf("Floor(3.14): %.0f\n", mathx.Floor(3.14)) fmt.Printf("Pow(2, 3): %.0f\n", mathx.Pow(2, 3)) fmt.Printf("Sqrt(16): %.0f\n", mathx.Sqrt(16)) }
Output: Abs(-3.14): 3.14 Ceil(3.14): 4 Floor(3.14): 3 Pow(2, 3): 8 Sqrt(16): 4
func Average ¶
func Average[T constraints.Integer | constraints.Float](ns ...T) float64
Average calculates the average of a slice of numbers
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Statistical calculations scores := []float64{85, 92, 78, 96, 88} avg := mathx.Average(scores...) median := mathx.Median(scores...) std := mathx.StandardDeviation(scores...) fmt.Printf("Average: %.2f\n", avg) fmt.Printf("Median: %.2f\n", median) fmt.Printf("Standard Deviation: %.2f\n", std) }
Output: Average: 87.80 Median: 88.00 Standard Deviation: 6.87
func BigFloatMul ¶
BigFloatMul multiplies two big.Float values using decimal precision
func Clamp ¶
Clamp clamps a value between min and max
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Utility functions value := 15.0 min := 0.0 max := 10.0 clamped := mathx.Clamp(value, min, max) fmt.Printf("Clamp(%.0f, %.0f, %.0f): %.0f\n", value, min, max, clamped) // Linear interpolation lerp := mathx.Lerp(0.0, 10.0, 0.5) fmt.Printf("Lerp(0, 10, 0.5): %.1f\n", lerp) }
Output: Clamp(15, 0, 10): 10 Lerp(0, 10, 0.5): 5.0
func CleanFloat ¶
CleanFloat removes trailing zeros and returns a clean float64
func CleanFloatString ¶
CleanFloatString removes trailing zeros and returns a clean string representation
func CompoundInterest ¶
CompoundInterest calculates compound interest
func DecimalMax ¶
DecimalMax returns the maximum decimal value
func DecimalMin ¶
DecimalMin returns the minimum decimal value
func DecimalSum ¶
DecimalSum returns the sum of decimal values
func FormatCurrency ¶
FormatCurrency formats a number as currency with specified decimal places
func FormatMoney ¶
FormatMoney formats a number as currency with thousands separator
func FormatMoneyInt ¶
FormatMoneyInt formats an int64 as currency with thousands separator
func Int64DivTrunc ¶
Int64DivTrunc truncates the division of two int64 values
func Int64MulFloat64 ¶
Int64MulFloat64 multiplies int64 and float64 using decimal precision
func Max ¶
func Max[T constraints.Ordered](ns ...T) T
Max returns the maximum value from a slice of numbers
func Median ¶
func Median[T constraints.Integer | constraints.Float](ns ...T) float64
Median calculates the median of a slice of numbers
func Min ¶
func Min[T constraints.Ordered](ns ...T) T
Min returns the minimum value from a slice of numbers
func ParseFloat ¶
ParseFloat safely parses a string to float64
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Safe string parsing value, err := mathx.ParseFloat("3.14") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Parsed value: %.2f\n", value) }
Output: Parsed value: 3.14
func Percentage ¶
Percentage calculates percentage of a value
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Calculate tip bill := 50.00 tipPercent := 18.0 tip := mathx.Percentage(bill, tipPercent) // Calculate total total := mathx.Add(bill, tip) fmt.Printf("Bill: $%.2f\n", bill) fmt.Printf("Tip (%.0f%%): $%.2f\n", tipPercent, tip) fmt.Printf("Total: $%.2f\n", total.Float64()) }
Output: Bill: $50.00 Tip (18%): $9.00 Total: $59.00
func RemoveTrailingZeros ¶
RemoveTrailingZeros removes trailing zeros from a float64 string representation
func RemoveTrailingZerosFixed ¶
RemoveTrailingZerosFixed removes trailing zeros from a float64 with fixed decimal places
func StandardDeviation ¶
func StandardDeviation[T constraints.Integer | constraints.Float](ns ...T) float64
StandardDeviation calculates the standard deviation of a slice of numbers
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](ns ...T) T
Sum returns the sum of a slice of numbers
func ToString ¶
ToString converts a float64 to string
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Various string conversion methods value := 3.14159 fmt.Printf("ToString: %s\n", mathx.ToString(value)) fmt.Printf("ToStringFixed(2): %s\n", mathx.ToStringFixed(value, 2)) fmt.Printf("ToStringBank(2): %s\n", mathx.ToStringBank(value, 2)) }
Output: ToString: 3.14159 ToStringFixed(2): 3.14 ToStringBank(2): 3.14
func ToStringBank ¶
ToStringBank converts a float64 to string with banker's rounding
func ToStringFixed ¶
ToStringFixed converts a float64 to string with fixed decimal places
Types ¶
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
Result represents a calculation result with chainable methods
func Add ¶
Add adds two float64 values using decimal precision and returns a Result
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Basic addition with precision result := mathx.Add(0.1, 0.2) fmt.Printf("0.1 + 0.2 = %.10f\n", result.Float64()) }
Output: 0.1 + 0.2 = 0.3000000000
Example (Chainable) ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Chainable operations result := mathx.Add(0.1, 0.2). Mul(10). Div(3, 2). Round(2). ToStringFixed(2) fmt.Printf("Result: %s\n", result) }
Output: Result: 1.00
Example (Clean) ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Remove trailing zeros result := mathx.Add(0.1, 0.2). Clean(). ToString() fmt.Printf("Clean result: %s\n", result) }
Output: Clean result: 0.3
func Mul ¶
Mul multiplies two float64 values using decimal precision and returns a Result
Example ¶
package main import ( "fmt" "github.com/go4x/goal/mathx" ) func main() { // Money formatting with thousands separator price := mathx.Mul(99.99, 1.15). Round(2). FormatMoney(2) fmt.Printf("Price: $%s\n", price) }
Output: Price: $114.99
func (Result) FormatMoney ¶
FormatMoney formats as currency with thousands separator
func (Result) ToStringBank ¶
ToStringBank returns the string with banker's rounding
func (Result) ToStringFixed ¶
ToStringFixed returns the string with fixed decimal places