Currency formatting

I struggle with correct formatting of a double variable as currency.

How do I display 23455.66 as €23.455,66 (Dutch locale) or 1711 as €1.711,00?

Does Integer.ToString not do what you want?

see
CType and
Currency.ToString(
https://documentation.xojo.com/api/data_types/currency.html#currency-tostring

1 Like

I do it this way, if it is only for this specific loacle:
msgbox ("€" + format(1234.56, "###,##0.00"))

Interesting that Ctype(double, currency). does not offer anything after the period (when we use TAB) but writing Ctype(double, currency).ToString(Locale) works.

I created an Issue for this.

Why not

msgbox (format(1234.56, "$###,##0.00")) // $ means the locale specific currency symbol.

In the US it would be $1,234.56, in holland it would be €1.234,56

Oh, have they rejoined the Empire? News to me! :laughing:

4 Likes

Hilarious typo! It originally showed £ rather than $. (shift-3 as opposed to shift-4 on a UK Mac Keyboard!)

Because if I sell something for 300 € this is $ 317, today :wink:

1 Like

I don’t understand the number has nothing to do with it. Give it what ever number you want. The point was that the $ symbol within the format stands for which ever the currently symbol is in the defined locale.

It wasn’t an attempt to explain international pricing or exchange rates.

Then you have to use $ or \€ this is possible, but I like to have that outside the format string.

the good thing at currency class is it provide both directions
Currency.FromString(value As String, locale As Locale = Nil) As Currency
but i remeber i wrapped my own method around

because of all the quirks that happened between xojo and the currency class, I made a long ago my own methods string.tocurrency and currency.tostring, with my own string manipulation and filtering adapted to my french locale. I still use them today…

1 Like

We’re being told that all you need to use is the $. The correct currency symbol for your locale will then be substituted.

As I said before, I cannot currently imagine a situation where I would want to have a different currency symbol set by the system locale.
The currency symbol is always dependent on the sum that is there. If the currency changes, the number also changes.

Just about any application that is only ever used in one country and doesn’t deal with international trade. For a very simple example. A tips app that calculates a desired percentage of the bill and want to present it’s output as “money”.

Using the built in $ operator you don’t have to alter the application for each country it is sold in. It happens automatically.

Thanks for all the answers but:
msgbox (format(1234.56, "$###,##0.00"))

It gives me 1.234,56 but without the currency sign…2023R3
Using the “$” doesn’t affect the formatting. Changing it into “€” has no effect either.

So it would seem, I’m sure it used to. However this is the only way I’ve managed to get it to work now:

Var n As Double = 1239.4567
MessageBox( n.ToString(Locale.current, "¤#,###.##") )

The ¤ symbol is representing the currency. It is work noting that it follows the locales formatting. So in some countries it could produce “1,239.46 €” and “£1,239.46” so the currency is automatically positioned by locale.

So it uses the locale’s currency symbol in the locale’s currency symbol position for that - but what about the separators?

For currency datatypes it looks like the following:

Var n As Currency = 1239.4567
Var FrCAlocale As New Locale("fr-CA")
Var NLlocale As New Locale("dut-nld")
MessageBox( n.ToString(Locale.current) ) // £1,239.46 (assuming UK like myself)
MessageBox( n.ToString(FrCAlocale) ) // 1 239,46 $
MessageBox( n.ToString(NLlocale) ) // € 1.239,46

Interestingly despite what the documentation states, it doesn’t seem to do what it claims using doubles:

Var n As Double = 1239.4567
Var FrCAlocale As New Locale("fr-CA")
Var NLlocale As New Locale("dut-nld")
MessageBox( n.ToString(Locale.current, "¤#,##0.00" ) ) // £1,239.46 (assuming UK like myself)
MessageBox( n.ToString(FrCAlocale, "¤#,##0.00" ) ) // $1 239,46
MessageBox( n.ToString(NLlocale, "¤#,##0.00" ) ) // €1.239,46

The “magic” only seems to operate with currency as you don’t specify a string format.

double the ¤¤ results in international currency abbreviations.

MessageBox( n.ToString(Locale.current, "¤¤#,##0.00" ) ) // GBP 1,239.46
MessageBox( n.ToString(FrCAlocale, "¤¤#,##0.00" ) ) // CAD 1 239,46
MessageBox( n.ToString(NLlocale, "¤¤#,##0.00" ) ) // EUR 1.239,46