Skip to content

sgns::ScaledInteger

Represents a decimal value using an integer scaled by 10^precision. More...

#include <ScaledInteger.hpp>

Public Types

Name
enum class ParseMode { Strict, Truncate}
Controls handling of extra fractional digits when parsing.

Public Functions

Name
outcome::result< std::shared_ptr< ScaledInteger > > New(uint64_t raw_value, uint64_t precision)
Create a ScaledInteger from a raw integer and precision.
outcome::result< std::shared_ptr< ScaledInteger > > New(double value, uint64_t precision)
Create a ScaledInteger from a double and precision.
outcome::result< std::shared_ptr< ScaledInteger > > New(const std::string & str, uint64_t precision, ParseMode mode =ParseMode::Strict)
Create a ScaledInteger from a string and precision.
outcome::result< std::shared_ptr< ScaledInteger > > New(const std::string & str)
Create a ScaledInteger from a decimal string by inferring precision.
uint64_t ScaleFactor(uint64_t precision)
Compute 10^precision as the scale factor.
outcome::result< uint64_t > FromString(const std::string & str, uint64_t precision, ParseMode mode =ParseMode::Strict)
Convert a numeric string to a raw scaled integer.
std::string ToString(uint64_t value, uint64_t precision)
Convert a scaled integer to a string representation.
outcome::result< uint64_t > FromDouble(double value, uint64_t precision)
Convert a double to a raw scaled integer.
outcome::result< uint64_t > Multiply(uint64_t a, uint64_t b, uint64_t precision)
Multiply two raw scaled integers.
outcome::result< uint64_t > Divide(uint64_t a, uint64_t b, uint64_t precision)
Divide two raw scaled integers.
outcome::result< uint64_t > ConvertPrecision(uint64_t value, uint64_t from, uint64_t to)
Change the precision of a raw scaled integer.
uint64_t Value() const
Get the raw scaled integer value.
uint64_t Precision() const
Get the precision (number of decimal places).
std::string ToString(bool fixedDecimals =true) const
Return this value as a string.
outcome::result< ScaledInteger > Add(const ScaledInteger & other) const
Add another ScaledInteger with matching precision.
outcome::result< ScaledInteger > Subtract(const ScaledInteger & other) const
Subtract another ScaledInteger with matching precision.
outcome::result< ScaledInteger > Multiply(const ScaledInteger & other) const
Multiply by another ScaledInteger with matching precision.
outcome::result< ScaledInteger > Divide(const ScaledInteger & other) const
Divide by another ScaledInteger with matching precision.
outcome::result< ScaledInteger > ConvertPrecision(uint64_t to) const
Convert this ScaledInteger to a different precision.

Detailed Description

class sgns::ScaledInteger;

Represents a decimal value using an integer scaled by 10^precision.

Internally stores the decimal as an integer multiplied by 10^precision. Example: raw_value = 12345, precision = 2 => decimal value 123.45

Public Types Documentation

enum ParseMode

Enumerator Value Description
Strict Fail if input has more fractional digits than precision.
Truncate Drop any extra fractional digits.

Controls handling of extra fractional digits when parsing.

Public Functions Documentation

function New

static outcome::result< std::shared_ptr< ScaledInteger > > New(
    uint64_t raw_value,
    uint64_t precision
)

Create a ScaledInteger from a raw integer and precision.

Parameters:

  • raw_value Integer representation scaled by 10^precision.
  • precision Number of decimal places (scale factor).

Return: Outcome containing shared_ptr to ScaledInteger or error.

function New

static outcome::result< std::shared_ptr< ScaledInteger > > New(
    double value,
    uint64_t precision
)

Create a ScaledInteger from a double and precision.

Parameters:

  • value Double value to convert.
  • precision Number of decimal places (scale factor).

Return: Outcome containing shared_ptr to ScaledInteger or error.

function New

static outcome::result< std::shared_ptr< ScaledInteger > > New(
    const std::string & str,
    uint64_t precision,
    ParseMode mode =ParseMode::Strict
)

Create a ScaledInteger from a string and precision.

Parameters:

  • str String representation of the decimal value.
  • precision Number of decimal places (scale factor).
  • mode Mode for handling extra fractional digits.

Return: Outcome containing shared_ptr to ScaledInteger or error.

function New

static outcome::result< std::shared_ptr< ScaledInteger > > New(
    const std::string & str
)

Create a ScaledInteger from a decimal string by inferring precision.

Parameters:

  • str String representation of the decimal value (e.g., "123.45"). In Strict mode, will fail if more than inferred precision.

Return: Outcome containing shared_ptr to ScaledInteger or error.

function ScaleFactor

static uint64_t ScaleFactor(
    uint64_t precision
)

Compute 10^precision as the scale factor.

Parameters:

  • precision Number of decimal places.

Return: Scale factor (10^precision).

function FromString

static outcome::result< uint64_t > FromString(
    const std::string & str,
    uint64_t precision,
    ParseMode mode =ParseMode::Strict
)

Convert a numeric string to a raw scaled integer.

Parameters:

  • str Numeric string with integer and fractional parts.
  • precision Number of decimal places.
  • mode Mode for handling extra fractional digits.

Return: Outcome containing raw scaled integer or error.

function ToString

static std::string ToString(
    uint64_t value,
    uint64_t precision
)

Convert a scaled integer to a string representation.

Parameters:

  • value Raw scaled integer.
  • precision Number of decimal places.

Return: String representation of the decimal value.

function FromDouble

static outcome::result< uint64_t > FromDouble(
    double value,
    uint64_t precision
)

Convert a double to a raw scaled integer.

Parameters:

  • value Double value to convert.
  • precision Number of decimal places.

Return: Outcome containing raw scaled integer or error.

function Multiply

static outcome::result< uint64_t > Multiply(
    uint64_t a,
    uint64_t b,
    uint64_t precision
)

Multiply two raw scaled integers.

Parameters:

  • a First raw scaled integer.
  • b Second raw scaled integer.
  • precision Number of decimal places.

Return: Outcome containing raw scaled integer product or error.

function Divide

static outcome::result< uint64_t > Divide(
    uint64_t a,
    uint64_t b,
    uint64_t precision
)

Divide two raw scaled integers.

Parameters:

  • a Dividend as raw scaled integer.
  • b Divisor as raw scaled integer.
  • precision Number of decimal places.

Return: Outcome containing raw scaled integer quotient or error.

function ConvertPrecision

static outcome::result< uint64_t > ConvertPrecision(
    uint64_t value,
    uint64_t from,
    uint64_t to
)

Change the precision of a raw scaled integer.

Parameters:

  • value Raw scaled integer at original precision.
  • from Original number of decimal places.
  • to Target number of decimal places.

Return: Outcome containing raw scaled integer at new precision or error.

function Value

uint64_t Value() const

Get the raw scaled integer value.

Return: Raw scaled integer.

function Precision

uint64_t Precision() const

Get the precision (number of decimal places).

Return: Number of decimal places.

function ToString

std::string ToString(
    bool fixedDecimals =true
) const

Return this value as a string.

Parameters:

  • fixedDecimals

  • true: always show all fractional digits (pad with zeros up to precision)

  • false: trim trailing '0's in the fractional part (and drop the '.' if no fraction remains)

Return: formatted string

function Add

outcome::result< ScaledInteger > Add(
    const ScaledInteger & other
) const

Add another ScaledInteger with matching precision.

Parameters:

Return: Outcome containing sum ScaledInteger or error.

function Subtract

outcome::result< ScaledInteger > Subtract(
    const ScaledInteger & other
) const

Subtract another ScaledInteger with matching precision.

Parameters:

Return: Outcome containing difference ScaledInteger or error.

function Multiply

outcome::result< ScaledInteger > Multiply(
    const ScaledInteger & other
) const

Multiply by another ScaledInteger with matching precision.

Parameters:

Return: Outcome containing product ScaledInteger or error.

function Divide

outcome::result< ScaledInteger > Divide(
    const ScaledInteger & other
) const

Divide by another ScaledInteger with matching precision.

Parameters:

Return: Outcome containing quotient ScaledInteger or error.

function ConvertPrecision

outcome::result< ScaledInteger > ConvertPrecision(
    uint64_t to
) const

Convert this ScaledInteger to a different precision.

Parameters:

  • to Target number of decimal places.

Return: Outcome containing new ScaledInteger or error.


Updated on 2026-03-04 at 13:10:43 -0800