0

I have deployed my .NET Core (v2.1) application onto my Ubuntu server (Ubuntu 18.04 LTS). The application target audience are UK based.

In C# I am doing:

@invoice.Amount.ToString("C")

Which formats the value as a currency based on system culture and should show something like £107.50, instead I get $107.50.

I checked the locale and I had en_US so I ran update-locale LANG=en_GB.utf8 and I restarted everything (kestrel, nginx and session). Now when I run the locale command, I get:

LANG=en_GB.utf8
LANGUAGE=
LC_CTYPE="en_GB.utf8"
LC_NUMERIC="en_GB.utf8"
LC_TIME="en_GB.utf8"
LC_COLLATE="en_GB.utf8"
LC_MONETARY="en_GB.utf8"
LC_MESSAGES="en_GB.utf8"
LC_PAPER="en_GB.utf8"
LC_NAME="en_GB.utf8"
LC_ADDRESS="en_GB.utf8"
LC_TELEPHONE="en_GB.utf8"
LC_MEASUREMENT="en_GB.utf8"
LC_IDENTIFICATION="en_GB.utf8"
LC_ALL=

But still, I get $107.50 instead of £107.50. What am I missing?

2
  • 1
    ToString( method in C# should accept second parameter, try @invoice.Amount.ToString("C",CultureInfo.CurrentCulture) or @invoice.Amount.ToString("C",CultureInfo.CurrentUICulture) or @invoice.Amount.ToString("C",CultureInfo.GetCultureInfo('en-GB')) or @invoice.Amount.ToString("C",CultureInfo.CreateSpecificCulture('en-GB')). Might require using System.Globalization. Commented Nov 28, 2018 at 13:41
  • That was it, please reply as Answer so that I can mark it as such. Commented Nov 28, 2018 at 13:55

1 Answer 1

1

Read Standard Numeric Format Strings (with examples in C#):

Standard numeric format strings are supported by some overloads of the ToString method of all numeric types. For example, you can supply a numeric format string to the Int32.ToString(FormatSpecifier) and Int32.ToString(FormatSpecifier, IFormatProvider) methods…

So .ToString() method should accept second parameter IFormatProvider (i.e. an object that supplies culture-specific formatting information) in addition to the Currency ("C") Format Specifier, try

  • @invoice.Amount.ToString("C",CultureInfo.CurrentCulture) or
  • @invoice.Amount.ToString("C",CultureInfo.CurrentUICulture) or
  • @invoice.Amount.ToString("C",CultureInfo.GetCultureInfo('en-GB')) or
  • @invoice.Amount.ToString("C",CultureInfo.CreateSpecificCulture('en-GB'))

(Might require using System.Globalization).

I can't provide running C# examples at present. However, I can exemplify use of (some) static properties and methods of the System.Globalization.CultureInfo class in PowerShell:

PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentCulture)
1 234,58 Kč

PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CurrentUICulture)
£1,234.58

PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::GetCultureInfo('en-US'))
$1,234.58

PS D:\PShell> 1234.578.ToString('C',[cultureinfo]::CreateSpecificCulture('de-DE'))
1.234,58 €
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.