J86
J86

Reputation: 15307

Locale issue with .NET Core application deployed on Ubuntu

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?

Upvotes: 0

Views: 1093

Answers (1)

JosefZ
JosefZ

Reputation: 30153

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 €

Upvotes: 1

Related Questions