Formatting currency value with GBP

Hi,
I guess this question is all about encoding which I think I understand after reading the docs on it. However, I cannot solve my small issue of adding a “£” symbol to a string.

I’m using value.ToString(“£0.00”) in each case to display monetary values, and the default encoding is confirmed as UTF-8 when I add a messagbox to check the string.

As per the attached screenshot, the right hand column containing only a formatted value is fine, but the middle column containing an expression adds the extra character before the £ symbol. A $ symbol works fine and I’ve also tried CHR(163) with the same incorrect result. I also just tried ChrByte() from the docs but it appears no longer valid?

Any help appreciated.
Thanks

I have no answer but if you want to increase your chance of getting help, tell us please:

  • the kind of Project (Web, iOS, …)
  • the Xojo Version
  • the kind of used Control (ListBox, TextArea, …)
  • the Operating System
  • how the Strings are created

Would "Starter Pack (" + Format(Value, "\£#.00") + ")" help?

Maybe show us your code? :slight_smile:

I’m not saying this is the solution to your problem (looks more like a Report issue but we need a sample project that you can Zip and upload here), but Currency.ToString adds the corresponding symbol if you provide a locale:

Var c As Currency = 123.45
Var s As String = c.ToString(New locale("en-UK")) // s = £123.45

Edit: for example using locale “fr-FR” we get 123,45 €

1 Like

Apologies :upside_down_face:

So I have Xojo 2025v1.1, with a Windows desktop project on Windows 11 Pro.
I’m using a ListBox control, adding a new row for a sale containing various fields including productID, quantity etc.

SecWindow.CurrentSaleList.AddRow("1",Item.ProductID.ToString,Item.Qty.ToString("0"),Item.LabelUnit, Item.LabelProduct + " (" + Item.PriceRRP.ToString("£0.00") + " each)",Item.SubTotal.ToString("£0.00"))

So this works fine in the last column:

Item.SubTotal.ToString("£0.00")

But this doesn’t for the column before:

Item.LabelProduct + " (" + Item.PriceRRP.ToString("£0.00") + " each)"

Thanks

I tried using Format as you suggested

SecWindow.CurrentSaleList.AddRow("1",Item.ProductID.ToString,Item.Qty.ToString("0"),Item.LabelUnit, Item.LabelProduct + " (" + Format(Item.PriceRRP,"\£#.00") + " each)",Format(Item.SubTotal,"\£#.00"))

Unfortunately, with the same result:

I’ve tried to replicate your issue in this project, without success:
untitled2.zip (4.1 KB)


Maybe you have an invisible char in your string. Did you try the following?

Item.PriceRRP and Item.SubTotal are not Currency, right?
What happens when you use only Item.PriceRRP.ToString without the format?

Edit:

I copy/paste the code but didn’t find an invisible character. I had found invisible characters on users code before. I’m not saying that there are not on OPs code but at least the code here shows none.

1 Like

Your sample works ok on my system as well :face_with_diagonal_mouth:

I’ve just added a Method to the Item Class called DescriptionPrice as follows

Return me.LabelProduct + "£"

..which still gives the same erroneous character.

However, changing it to the following:

Return "£"

..correctly displays just a pound sign.

There are no invisible characters on Item.LabelProduct as they are database fields and I’ve checked the source and also tried multiple ‘products’ so it’s not jsut one corrupt text field.
Very strange.

I’ve also just realised that I use a derived string containing a formatted price in another ‘Discount’ row which works perfectly so even more confused..

I would recommend creating a copy of the project, deleting everything that isn’t allowed to leave the house, and (assuming the bug is still visible in the copy of the project) opening an issue.

It might be a Xojo bug or something Xojo could do differently/better.

Please, read the Currency entry from the documentation.

You have to use la locale:


Var CommonWealthLocale as New Locale ("en-GB")

in your ToString convertion.

This will format correctly the decimal charater and the currency character.

I learned this lesson the hard way (I could not understandwhy I HAVE to do that…)

What type of variable or property is me.LabelProduct? String, Text, Variant?
Can you check the encoding of that variable or property before adding in the pound sign?

I wonder if that string building is changing the encoding. I tried to reproduce your issue in a simple project, but because I’m using literals everything is UTF8 and working correctly.

Here’s what I’ve been playing with:

var PriceRRP as Double = 1.234
var s as String = " (" + PriceRRP.ToString("£0.00") + " each)"

if s.Encoding = nil then break
if s.Encoding = Encodings.UTF8 then break
1 Like

OP mentions currency but the Currency type is not used. Pulling from a database and using Doubles instead that’s why the format with the sign is needed.

Currency ToString does not allow format pattern.

I can bet that Item.LabelProduct is pulled from a database and is not UTF8.

Here is an example with a string with Nil encoding:

Var priceRRP As Double = 3.45
Var labelproduct As String = "Starter Pack"
labelproduct = labelproduct.DefineEncoding(Nil)
ListBox1.AddRow("1 x", labelproduct + " (" + pricerrp.ToString("£0.00") + " Each)",pricerrp.ToString("£0.00"))

Not sure what encoding you have that makes to only show a strange character before the symbol.

2 Likes

LabelProduct is just a string and I think you’re right, formulating a string must be changing the encoding.

Some html posts explain the erroneous character as non-UTF8 so by using ASCII it displays correctly.

I’ve modified the method as follows and it now works:

Dim s as String
s=me.LabelProduct + " (" + me.PriceRRP.ToString("£0.00") + " each)"
s=DefineEncoding(s,encodings.ASCII)
Return s

I don’t really understand as the pound sign was displayed in any case?

You need to check the encoding you have pulling from the database.

In one of my Projects i have to pull Doubles as a Currency Value from a mySQL Database and run into (expected) rounding issues. (Please don’t ask why i have to do this). :slight_smile:
So i made my own Truncate Method for Currency Values, to work around those rounding issues. :smiley:

Public Sub Truncate(Extends  ByRef Value As Currency, DecimalPlaces As Integer)
  Var Multiplier As Integer = Pow(10, DecimalPlaces)
  If Value < 0 Then
    Value = Ceil(Value * Multiplier) / Multiplier
  Else
    Value = Floor(Value * Multiplier) / Multiplier
  End
End Sub

1 Like

So are we saying, the database field in the string expression is setting the encoding; as opposed to UTF8 which is the Xojo default? That would make sense.

Is there best-practice around this at all? The data is actually read into an array when the app opens so should I be setting the encoding at that point?

Every time you pull information from a database (or save to the database) you need to make sure you are using the correct encoding.

1 Like

It is (regarding Xojo) without a defined encoding (=NIL). You have to tell Xojo of which encoding it is. Like in this example:

Reading from Database:

data.Column("customer_name").StringValue.DefineEncoding(Encodings.ISOLatin1)

Writing to Database:

db3.ExecuteSQL("UPDATE current_cards SET password=? WHERE ccard_id=?", _
Password.ConvertEncoding(Encodings.ISOLatin1), _
Ticketnummer.ConvertEncoding(Encodings.ISOLatin1))

(Yes, in my example it’s allowed to save a Password in Plain Text) :wink:

1 Like

So just to be clear when reading data..we need to specify the field in the database is encoded to ISOLatin1 and then XOJO stores with the default (UTF8).
Then when writing, we need to convert back to the database encoding.

Lastly, does this apply to all fields or just text as your example implies Ticketnummer may be an ID/value?

Sorry, just want 100% clarity in my brian :slightly_smiling_face:

1 Like