HANumeric class

class HANumeric

This class represents a numeric value that simplifies use of different types of numbers across the library.

Public Functions

HANumeric()

Creates an empty number representation.

HANumeric(const float value, const uint8_t precision)

Converts the given float into number representation of the given precision. If the precision is set to zero the given float will be converted into integer.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const int8_t value, const uint8_t precision)

Converts the given int8_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const int16_t value, const uint8_t precision)

Converts the given int16_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const int32_t value, const uint8_t precision)

Converts the given int32_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const uint8_t value, const uint8_t precision)

Converts the given uint8_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const uint16_t value, const uint8_t precision)

Converts the given uint16_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

HANumeric(const uint32_t value, const uint8_t precision)

Converts the given uint32_t into number representation of the given precision. If the precision is greater than zero the given value will be converted to float.

Parameters
  • value – The value that should be used as a base.

  • precision – The number of digits in the decimal part.

inline void operator=(const HANumeric &a)
inline bool operator==(const HANumeric &a) const
uint32_t getPrecisionBase() const

Returns multiplier that used to generate base value based on the precision. The multiplier is generated using the formula: pow(precision, 10).

uint8_t calculateSize() const

Returns size of the number

uint16_t toStr(char *dst) const

Converts the number to the string.

Note

The dst size should be calculated using HANumeric::calculateSize() method plus 1 extra byte for the null terminator.

Parameters

dst – Destination where the number will be saved. The null terminator is not added at the end.

Returns

The number of written characters.

inline bool isSet() const

Returns true if the base value is set.

inline void setBaseValue(int64_t value)

Sets the base value without converting it to the proper precision.

inline int64_t getBaseValue() const

Returns the base value of the number.

inline void setPrecision(const uint8_t precision)

Sets the precision of the number (number of digits in the decimal part).

Parameters

precision – The precision to use.

inline uint8_t getPrecision() const

Returns the precision of the number.

inline void reset()

Resets the number to the defaults.

inline bool isUInt8() const
inline bool isUInt16() const
inline bool isUInt32() const
inline bool isInt8() const
inline bool isInt16() const
inline bool isInt32() const
inline bool isFloat() const
inline uint8_t toUInt8() const
inline uint16_t toUInt16() const
inline uint32_t toUInt32() const
inline int8_t toInt8() const
inline int16_t toInt16() const
inline int32_t toInt32() const
inline float toFloat() const

Public Static Functions

static HANumeric fromStr(const uint8_t *buffer, const uint16_t length)

Deserializes number from the given buffer. Please note that the class expected buffer to contain the base number. For example, deserializing 1234 number and setting precision to 1 results in representation of 123.4 float.

Parameters
  • buffer – The buffer that contains the number.

  • length – The length of the buffer.

Public Static Attributes

static const uint8_t MaxDigitsNb

The maximum number of digits that the base value can have (int64_t).

Private Functions

explicit HANumeric(const int64_t value)

Private Members

bool _isSet
int64_t _value
uint8_t _precision