Managing monetary values in your applications requires some special care. Here are some tips to help out.

First, for monetary values, don’t use Double. A double is a floating-point number, which means it can have accuracy problems. This is an issue with all floating point numbers and not something specific to Xojo, but it is important to understand.

Instead, you should use the Currency data type. This data type is specifically designed for dealing with, well, currency. You can also use an Integer and then convert your numbers to decimals for display. How would Integer work? Using math. You simply multiply all your calculations by 100 to remove the decimal portion. For display purposes, you divide by 100 and format. But this is usually needless extra work.

There are several functions in Xojo that help with formatting of your currency values: CDbl, Format, CStr, Str and Val.

Str and Val are not locale-savvy. This means they always use “.” as the decimal separator, for example. Generally speaking, when you are saving data to a file or database, you do not want it to be in a specific locale. You want it to be generic so that it can be read and processed correctly every time.

You only should be dealing with specific locales when you are displaying information. In these cases, use the Format and CDbl functions.

Here is a summary of the functions:

• Str: Converts a number to a string value (not localized) using an optional format string.
• Val: Converts a string to a number value (not localized). Conversion stops at the first non-numeric character.
• Format: Converts a number to a string value (localized) using a format string.
• CStr: Converts a variety of non-string values to a string value (localized). For numbers, you should use Format.
• CDbl: Converts a string to a number value (localized). If the string contains thousands separators, this function will correctly parse the string into a number. Use CDbl to convert user-entered text into numbers.