Display value (decimal) without rounding

3

I have a database that has the following values:

48.205864

So, when I simply make a select , and bring it to the screen, it looks like this:

48,21

What do I need to do so that the value is not rounded up in C #, and displayed correctly the same is in the database?

extra: the select , I am using ef , as below:

public ActionResult Index()
        {
            return View(db.ipca.ToList().OrderBy(i => i.data));
        }

In my model:

public decimal valor { get; set; }
    
asked by anonymous 28.09.2016 / 04:20

1 answer

7

After much research, I found the article (according to the link below) that specifies and exemplifies my need. What I had to do was add the annotation in the corresponding property value, in my model :

[DisplayFormat(DataFormatString = "{0:0,0.000000}")]'
public decimal valor { get; set; }

Using the mask that I need, I was able to print the correct value of: 48.205864

The original link to the article: link In order not to risk the link being deprecated someday, the explanations below, which I credit the author of the link informed, follow below.

Setting the maximum allowed decimal places To format a number with a maximum of two decimal places, use the string format {0: 0. ##} as shown in the following example:

string.Format("{0:0.##}", 123.583); // "123.58"
string.Format("{0:0.##}", 123.586); // "123.59"
string.Format("{0:0.##}", 123.58);  // "123.58"
string.Format("{0:0.##}", 123.5);   // "123.5"
string.Format("{0:0.##}", 123.0);   // "123"

Defining a fixed size of decimal places This is similar to the above example, but instead of hashes ('#') in the format string, we will use zeros ('0') as follows:

string.Format("{0:0.00}", 123.583); // "123.58"
string.Format("{0:0.00}", 123.586); // "123.59"
string.Format("{0:0.00}", 123.58);  // "123.58"
string.Format("{0:0.00}", 123.5);   // "123.50"
string.Format("{0:0.00}", 123.0);   // "123.00"

The thousands separator To format decimal using the thousands separator, use the format string {0: 0,0} as shown in the following example:

string.Format("{0:0,0.00}", 1234256.583); // "1,234,256.58"
string.Format("{0:0,0}", 1234256.583);    // "1,234,257"

Defining a fixed number of digits before the decimals To set a minimum number of three digits before the decimals, use the format string {0: 000. #}.

string.Format("{0:00.000}", 1.2345);    // "01.235"
string.Format("{0:000.000}", 12.345);   // "012.345"
string.Format("{0:0000.000}", 123.456); // "0123.456"

Alignment To specify the alignment to the formatting method, you should write its format as follows. Note that comma (',') was used to specify the number of characters used for the alignment. 0, [no. and if you want to fill with zeros {0, [no. of characters]: 00.00}

string.Format("{0,7:##.00}", 2.356);  // "   2.36"
string.Format("{0,-7:##.00}", 2.356); // "2.36   "
string.Format("{0,7:00.00}", 2.356);  // "  02.36"
string.Format("{0,-7:00.00}", 2.356); // "02.36  "

Positive, negative, and zero numbers You can include different formats for positive, negative, and zero numbers using the semicolon (';'). Format string:

//{0:[positive];[negative];[zero]}
string.Format("{0:000.000;(000.000);zero}", 23.43);  // "023.430"
string.Format("{0:000.000;(000.000);zero}", -23.43); // "(023.430)"
string.Format("{0:000.000;(000.000);zero}", 0.0);    // "zero"

Some pre-defined formats

string.Format("{0:C}", 1532.236);  // "£1,532.24"
string.Format("{0:C}", -1532.236); // "-£1,532.24"
string.Format("{0:E}", 1532.236);  // "1.532236E+003"
string.Format("{0:E}", -1532.236); // "-1.532236E+003"
string.Format("{0:F}", 1532.24);   // "1532.24"
string.Format("{0:F}", -1532.24);  // "-1532.24"
string.Format("{0:G}", 1532.236);  // "1532.236"
string.Format("{0:G}", -1532.236); // "-1532.236"
string.Format("{0:N}", 1532.236);  // "1,532.24"
string.Format("{0:N}", -1532.236); // "-1,532.24"
string.Format("{0:P}", 0.1532);    // "15.32 %"
string.Format("{0:P}", -0.1532);   // "-15.32 %"
string.Format("{0:R}", 1532.236);  // "1532.236"
string.Format("{0:R}", -1532.236); // "-1532.236"
    
28.09.2016 / 15:00