0

In my view which I am exporting to excel, I display the value of this variable

VeryGoodPercentage = subQst.Question.ReponseList.Where(
            r => r.ReponseValeur == 4 &&
            Model.SelectedGroupContactList.Select(c => c.ContaId).Contains(r.ContaId.Value)
            ).ToList().Count() * 100
            /
            denominator;

And I display this way

<td class=xl76 align=right width=69 style='width:52pt'>@Math.Round(VeryGoodPercentage)%</td>

When I display the view without exporting to excel, everything works just fine. But when I open the exported Excel file, I found the value of that variable not rounded.

For examlpe :

In the view : 36%

In the excel file : 0.36

I use this line to generate the file:

Response.AddHeader("Content-Type", "application/vnd.ms-excel");   
kbaccouche
  • 4,575
  • 11
  • 43
  • 65
  • You seem to be talking about some exported Excel file - could you please show the relevant code that you are using in order to generate this file? – Darin Dimitrov Sep 12 '12 at 10:13
  • I edited the post, I put that line in my view – kbaccouche Sep 12 '12 at 10:19
  • 1
    36% and 0.36 would be the same - just excel did not fomat this as a percentage; i don't see that this is a rounding problem. – Jook Sep 12 '12 at 10:25
  • Agreed with Jook. That's a problem with formatting in Excel, it has strictly nothing to do with the Math.Round function. – Darin Dimitrov Sep 12 '12 at 10:28
  • Thanks for replying, but is there a way to make Excel format it as a percentage without doing it from excel itself ? – kbaccouche Sep 12 '12 at 10:41

2 Answers2

0

As stated:

36% and 0.36 should be the same - just excel did not fomat this as a percentage or did indeed interpreted 36 as a percentage - which will result in 0.36 as a number.

I don't see that this is a rounding problem, it is formatting or input or both.

Depending on what values @Math.Round(VeryGoodPercentage) produce - 0.xx or XX.XX - with % sign or without, excel will interpret this differently.

Some examples:

=0.36 will result in a number of 0.36

=36 will result in a number of 36

no suprises so far, but now:

=0.36% will result to 0.0036

=36% will result to 0.36

BUT those were Formulars, now if you would just use F2 on an empty 'default' formatted cell and write into it:

36% -> you would format this as percentage, which would display this as 36%, but if you would format it as 'default', then it would be the number 0.36!

0.36% -> would behave like 36%

hope this clears more than it confuses ;) good luck to you!

edit: and to make this even more fuzzy, in my country . is the symbol for thounsands, so you would have to use , instead.

edit: with VBA you could setup your column or cell to use a specific numberformat like this:

Table1.Columns("A").NumberFormat = "0.00%"
Jook
  • 4,564
  • 3
  • 26
  • 53
0

I finally found the solution here

Format HTML table cell so that Excel formats as text?

just add this css param to the tag :

mso-number-format:Percent;
Community
  • 1
  • 1
kbaccouche
  • 4,575
  • 11
  • 43
  • 65