5

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!!!

5

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Thank you! I now use this in my razor view:
@String.Format("{0:N0}", @Model.PreisVon)
1

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));
     }
}

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.