2

For display purposes, is it possible to set a site global rule for the amount of digits to display after the decimal separator ASP.NET MVC 4? In the event that it is, how does one achieve this?

The optimal method would be a culture related rule specified in web.config which affects all printed decimals.

Maritim
  • 2,111
  • 4
  • 29
  • 59

2 Answers2

0

Based on this answer, Adjusting decimal precision, .net the number of digits displayed depends on what you enter in the first place.

My guess however is that you are asking how to simply round the decimal? In which case you can just use Math.Round.

This quick answer as far as I am aware however is no, I am not aware of built-in global mechanism of limiting the number of digits displayed when outputting the number to the screen.

Community
  • 1
  • 1
user460667
  • 1,870
  • 3
  • 19
  • 26
  • I appreciate the answer. The optimal method would be a culture related rule specified in web.config which affects all printed decimals. – Maritim Dec 16 '13 at 08:35
0

When you use the .ToString(format) method to convert the number values to strings and you specify the "N" format, then you can use CultureInfo to set the number of decimal places:

var ci = new System.Globalization.CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
ci.NumberFormat.NumberDecimalDigits = 1;
Thread.CurrentThread.CurrentCulture = ci;

decimal dm = 20.12345m;
double db = 20.12345;
Console.WriteLine(dm.ToString("N"));
Console.WriteLine(db.ToString("N"));

Both display with 1 decimal.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111