DataType

Currency


Description

A a decimal number that holds 15 digits to the left of the decimal point and 4 digits to the right. The default value of a Currency is 0.0.

Methods

Name

Parameters

Returns

Shared

FromString

value As String, locale As Locale = Nil

Currency

ToString

locale As Locale = Nil

String

Method descriptions


Currency.FromString

FromString(value As String, locale As Locale = Nil) As Currency

Converts a String value containing a number that can be represented as a Currency to a Currency value.

This method is shared.

If no locale is specified, then Locale.Raw is used.

If value cannot be converted to a Currency (because it contains non-numeric characters, for example), then a RuntimeException is raised.

Convert values to the Currency type:

Var userValue As String
userValue = "123.45"

Var c As Currency
c = Currency.FromString(userValue)

Var locale As New Locale("en-US")
Var value As Currency

userValue = "$123.45"
value = Currency.FromString(userValue, locale) ' value = 123.45

Currency.ToString

ToString(locale As Locale = Nil) As String

Converts a Currency value to a String value using the specified locale.

To display the Currency symbol, you need to pass in a locale. The Currency symbol is displayed before or after the number as specified by the locale settings.

If no locale is specified, then Locale.Raw is used.

Convert Currency to Text:

Var c As Currency = 123.45

Var s As String
s = c.ToString ' s = "123.45"

In order to display the Currency symbol you need to provide a locale:

Var c As Currency = 123.45

Var s As String
Var locale As New Locale("en-US")
s = c.ToString(locale) ' s = $123.45

This code displays a value using French Canadian locale with the Currency symbol after the number:

Var c As Currency = 123.45

Var s As String
Var locale As New Locale("fr-CA")
s = c.ToString(locale) ' s = 123,45 $

Notes

Currency can contain values from -922337203685477.5808 to 922337203685477.5807. It uses 8 bytes.

Currency is a 64-bit fixed-point decimal. This means that Currency does not have some of the rounding issues that are inherent in floating-point numbers stored in Double.

Use Currency instead of Double when storing monetary values. Since Double is a standard double-precision floating-point number, it is unable to store some specific decimal values. The Currency type avoids these issues, which can be particularly important with rounding and when dealing with money.

Sample code

Assign a value to a Currency variable:

Var c As Currency
c = 42.56

Compatibility

All project types on all supported operating systems.

See also

Double data type