Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decimal.ToString("C") produces ¤ currency symbol on Linux

I have an ASP.NET Core 2.1 project where I am rendering some currency numbers through a Razor HTML page.

View Model

class MyModel
{
    public decimal Money { get; set; } = 1.23
}

Razor Page

@model MyModel
<p>@Model.Money.ToString("C")</p>

This project is deployed to Azure App Service.

On a Windows App Service plan (and my local Windows 10 machine), this produces "$1.23" as expected. However, if I deploy the same project to a Linux App Service Plan it renders "¤1.23".

According to Google:

The currency sign (¤) is a character used to denote an unspecified currency.

Any idea what there is a difference between the two OS here? Do I need to explicitly set the culture or something on Linux?

like image 665
kspearrin Avatar asked Jul 11 '19 16:07

kspearrin


People also ask

What is the Tostring format code for currency?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier.

What type of number format is used to display values as a currency?

Tip: To quickly apply the Currency format, select the cell or range of cells that you want to format, and then press Ctrl+Shift+$. Like the Currency format, the Accounting format is used for monetary values. But, this format aligns the currency symbols and decimal points of numbers in a column.

Which of the following objects have an effect on .NET handling dates formatting and Unformatting issues currencies etc?

CurrentCulture handles dates,currencies,sorting and formatting issues in Dot Net.


1 Answers

This is caused by that the linux did not configure locale.

You could set the thread culture in your program like below:

public class Program
{
    public static void Main(string[] args)
    {
        CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}
like image 159
Edward Avatar answered Sep 22 '22 15:09

Edward