Display (decimal) value without rounding

Asked

Viewed 4,502 times

3

Hello, using a database mysql, with Asp.net mvc c#, have in the database the following value (of decimal type):

48.205864

So when I just make one select, and bring it to the screen, is displayed that way:

48.21

What I need to do for q the value is not rounded in C#, and displayed correctly equal is in the database?

extra: the select, I’m using the Ef, as below:

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

In my model:

public decimal valor { get; set; }
  • 1

    How is configured your decimal in the database, example decimal(18,2)?

  • @Virgilionovic for these data, the database is configured as decimal(15,6)

1 answer

9


After a lot of research, I found the article (according to the link below) that specifies and exemplifies well 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 I need, I was able to print on the screen the correct value of: 48.205864

The original link to the article: https://goo.gl/PpOZVo In order not to run the risk of the link being deactivated some day, follow below the explanations, which I give credit to the author of the link informed.

Setting the maximum permitted of decimals 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 decimals This is similar to the example above, but instead of hashes (? #') in the formatting string, let’s 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 separator of a thousand To format decimal using the mile 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, write its format as follows. Note that a comma was used to specify the number of characters used for the alignment. {0,[no. of characters]} and 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 dot and comma (?;'). 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 predefined 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"
  • 1

    Sorry, @Ericosouza, but, this does not answer the general context of your question that has a ORM (extra) among all this. This you explained is correct, but in order for the ORM to be able to use this configuration, it needs to know the configuration.

  • 1

    Well remembered @Virgilionovic, I will update the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.