numbers

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2026 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package numbers provides numeric types, utilities, and range abstractions for rounding, scaling, and yield adjustment calculations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RoundToDecimalPlaces

func RoundToDecimalPlaces(value float32, precision uint8) float32

RoundToDecimalPlaces rounds a float32 value to the specified number of decimal places.

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform/numbers"
)

func main() {
	result := numbers.RoundToDecimalPlaces(3.14159, 2)
	fmt.Println(result)
}
Output:
3.14

func Scale

func Scale(value, factor float32, precision ...uint8) float32

Scale multiplies a value by a scaling factor and rounds to the specified precision (default: 2). Useful for scaling quantities by a factor. For example, Scale(2.5, 2.0) would return 5.0 (doubling the quantity).

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform/numbers"
)

func main() {
	// Double a quantity of 2.5
	result := numbers.Scale(2.5, 2.0)
	fmt.Println(result)
}
Output:
5

func ScaleToYield

func ScaleToYield(originalValue float32, originalYield, desiredYield int, precision ...uint8) float32

ScaleToYield scales a quantity from an original yield to a desired yield. The optional precision parameter specifies the number of decimal places to round to (default: 2). For example, ScaleToYield(2.0, 4, 6) returns 3.0 (scaling from 4 units to 6).

Example
package main

import (
	"fmt"

	"github.com/primandproper/platform/numbers"
)

func main() {
	// Scale from 4 servings to 6 servings
	result := numbers.ScaleToYield(2.0, 4, 6)
	fmt.Println(result)
}
Output:
3

Types

type MinRange added in v0.0.2

type MinRange[T Numeric] struct {
	Min T  `json:"min"`
	Max *T `json:"max,omitempty"`
}

MinRange represents a range with a required minimum and optional maximum.

func (*MinRange[T]) ValidateWithContext added in v0.0.2

func (x *MinRange[T]) ValidateWithContext(ctx context.Context) error

type Numeric added in v0.0.2

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

Numeric is a constraint for all built-in numeric types.

type OpenRange added in v0.0.2

type OpenRange[T Numeric] struct {
	Min *T `json:"min,omitempty"`
	Max *T `json:"max,omitempty"`
}

OpenRange represents a range where both minimum and maximum are optional.

type OpenRangeUpdateRequestInput added in v0.0.2

type OpenRangeUpdateRequestInput[T Numeric] struct {
	Min *T `json:"min,omitempty"`
	Max *T `json:"max,omitempty"`
}

OpenRangeUpdateRequestInput represents an update request for an open range.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL