C#: Display if a percentage is a negative value or not
-
07-06-2021 - |
Pergunta
Simple one here. I am calculating the percentage difference between two values. I want to display them on a web page. The calculation occurs within my class file, e.g.
public decimal UnitPercentageDifference { get { return (decimal)ComparativeTotalSold / (decimal)BaseTotalSold; } }
In my View then, I display the value like this:
<p><%: String.Format("{0:0.0%}",Model.OverallSummary.UnitPercentageDifference) %> increase/decrease on previous month</p>
I want to be able to display a minus ( - ) sign if the value is negative, however this doesn't happen and it just looks like a normal positive percentage.
Is there a way to do this?
Solução
Try this mask {0:0.0%;-0.0%}
From here (see about ; seperator)
EDIT: Did some testing - you should get correct negative output with your existing mask. So check your calculation routines.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow