Currency
💰 Currency Struct
Representation of monetary value.
Namespace:
Esper.Inventool.Currencies
Access:public
Type:struct
Attributes:[Serializable]
📦 Fields
private
strictTypeError
string
(const)
Error message when strict type enforcement fails.
public
identity
CurrencyIdentity
The identity of the currency.
public
value
Value
The currency value.
public
stringValue
string
The value as a string.
🔧 Constructors
public
Currency(CurrencyIdentity identity, Value value)
Creates a currency with identity and value.
public
Currency(CurrencyIdentity identity, decimal value)
Creates a currency with identity and decimal value.
public
Currency(CurrencyIdentity identity, int value)
Creates a currency with identity and integer value.
public
Currency(CurrencyIdentity identity, float value)
Creates a currency with identity and float value.
public
Currency(CurrencyIdentity identity)
Creates a currency with identity only, initializes with starting values.
🔍 Properties
public
DecimalValue
decimal
The decimal value (inside nested Value
). Stored internally as a string.
public
integerValue
int
The integer value (inside nested Value
).
public
floatValue
float
The float value (inside nested Value
).
🧰 Methods
public
SetValue(decimal value)
void
Sets the value as decimal. Throws if strict type mismatch.
public
SetValue(int value)
void
Sets the value as integer. Throws if strict type mismatch.
public
SetValue(float value)
void
Sets the value as float. Throws if strict type mismatch.
public
SetValueWithConversion(decimal value)
void
Sets the value with conversion to the correct type.
public
SetValueWithConversion(int value)
void
Sets the value with conversion to the correct type.
public
SetValueWithConversion(float value)
void
Sets the value with conversion to the correct type.
public
IsComparable(Currency other)
bool
Checks if another currency can be compared with this one.
public
ToString()
string
Converts the value to a string.
public
ToStringWithSymbol()
string
Converts the value to a string with its symbol.
public
ToStringWithCode()
string
Converts the value to a string with its code.
public
Equals(object obj)
bool
Checks equality with another object.
public
GetHashCode()
int
Returns the hash code of the value.
➗✖️➕➖ Operator Overloads
/
(Currency a, decimal value)
<br>(Currency a, float value)
<br>(Currency a, int value)
<br>(Currency a, Currency b)
Division between Currency
and numeric types or another Currency
.
*
(Currency a, decimal value)
<br>(Currency a, float value)
<br>(Currency a, int value)
<br>(Currency a, Currency b)
Multiplication between Currency
and numeric types or another Currency
.
+
(Currency a, decimal value)
<br>(Currency a, float value)
<br>(Currency a, int value)
<br>(Currency a, Currency b)
Addition between Currency
and numeric types or another Currency
.
-
(Currency a, decimal value)
<br>(Currency a, float value)
<br>(Currency a, int value)
<br>(Currency a, Currency b)
Subtraction between Currency
and numeric types or another Currency
.
==
(Currency a, Currency b)
Equality comparison between two currencies.
!=
(Currency a, Currency b)
Inequality comparison between two currencies.
>
(Currency a, Currency b)
Greater-than comparison between two currencies.
<
(Currency a, Currency b)
Less-than comparison between two currencies.
>=
(Currency a, Currency b)
Greater-than-or-equal comparison between two currencies.
<=
(Currency a, Currency b)
Less-than-or-equal comparison between two currencies.
🧩 Nested Structs
public struct Value
public struct Value
Represents a currency value.
public
DecimalValue
decimal
The decimal value, stored internally as a string.
public
integerValue
int
The integer value.
public
floatValue
float
The float value.
private
decimalValueAsString
string
The decimal value represented as a string. Serialized for Unity.
public
UpdateDecimalValue()
void
Updates the decimal value from the float value.
Creating a Currency
To create a Currency
, you need a reference to a Currency Identity.
// Create a currency with 0 value
Currency myCurrency = new Currency(myCurrencyIdentity, 0);
Managing Values
A Currency
uses the Value
struct to support multiple numeric value types. The numeric type used by a Currency
depends on it's CurrencyIdentity
(int
, float
, or decimal
).
If you have strictCurrencyValueTypes
enabled in Inventool Settings, this will throw an error when trying to change the value using the incorrect numeric type.
Set the Value
To set a new value, you can use the SetValue
method.
// Sets the value to 100
myCurrency.SetValue(100);
Alternatively, you can use SetValueWithConversion
, which is similar to SetValue
, except it ignores the strictCurrencyValueTypes
settings field.
myCurrency.SetValueWithConversion(100);
Operators
You can use operators to perform mathematical operations with currencies. Strict value types may apply.
// Add 100
myCurrency += 100;
// Subtract 100
myCurrency -= 100;
// Double the value
myCurrency *= 2;
// Halve the value
myCurrency /= 2;
Last updated