john.1020
john.1020

Reputation: 135

Asp.NET MVC Razor Format Decimal? to #.###,##

I'm currently working on an ASP.NET MVC 4.5 application.

I try to render a property from my model as a correct numeric value:

The property in the ViewModel:

public decimal? Price { get; set; }

the data from the DB looks like this: 99999,99

My desired format would look like this: 99.999,99

In my razor View I use the property like this:

@Model.Price

Unfortunately it looks still like this: 99999.99

Do you know how I can format that decimal? value correctly on my view?

Is there also a solution without using a display template?

Thanks!!!

Upvotes: 5

Views: 9401

Answers (2)

Ali Shawky
Ali Shawky

Reputation: 104

Please Try Using This Guide

using System;
using System.Globalization;

public class TestClass
{
     public static void Main()
     {
         int i = 100;

         // Creates a CultureInfo for English in Belize.
         CultureInfo bz = new CultureInfo("en-BZ");
         // Displays i formatted as currency for the bz.
         Console.WriteLine(i.ToString("c", bz));

         // Creates a CultureInfo for English in the U.S.
         CultureInfo us = new CultureInfo("en-US");
         // Display i formatted as currency for us.
         Console.WriteLine(i.ToString("c", us));

         // Creates a CultureInfo for Danish in Denmark.
         CultureInfo dk = new CultureInfo("da-DK");
         // Displays i formatted as currency for dk.
         Console.WriteLine(i.ToString("c", dk));
     }
}

Upvotes: 1

Daniel Manta
Daniel Manta

Reputation: 6718

Try this

decimal value = 99999.99M;
string display = value.ToString("N2", CultureInfo.GetCultureInfo("es"));

Displays

99.999,99

I have tried all the values below:

"C" is used for displaying currency in your culture

"C2" currency with two digits after decimal point

"C2", culture: I'm using a culture which decimal point is comma

"N2", culture: Because I want to show only number, without currency symbol

Upvotes: 5

Related Questions