I have some percentages and I don't know how to remove the zeroes. Meaning if I have 5.00% I want it to show 5%, however if I have 5.20% I want it to show 5.20%.
I go through each member from a model and I have <span>@item.percentage</span>
. How do I make it show up properly?
Asked
Active
Viewed 773 times
2

Hamid Pourjam
- 20,441
- 9
- 58
- 74

razzv
- 85
- 8
-
Can you please show the relevant code as well? – Soner Gönül Dec 10 '15 at 08:50
-
What is the type you are storing percentages in? Double? Decimal? – Hamid Pourjam Dec 10 '15 at 08:50
-
the percentage is decimal – razzv Dec 10 '15 at 08:54
-
just remove the percentage sign and use- `Math.Round(inputValue, 0);` – sud Dec 10 '15 at 08:54
-
1None of the answers in the "duplicate" question actually answer this question because they all turn "5.2" into "5.2" and not (as the OP requested) "5.20" – Matthew Watson Dec 10 '15 at 08:54
-
I vote to reopen this question. – Hamid Pourjam Dec 10 '15 at 08:57
-
What should "5.999" be returned as? "6.00%" or "6%" or something else? – Matthew Watson Dec 10 '15 at 09:09
3 Answers
1
You can check if the number has decimal places or not and generate appropriate result.
public static string MyDoubleToString(double d)
{
// preventing rounding
// if you want 5.9999 becomes 6 then comment the line below
d = Math.Truncate(d * 100) / 100;
return $"{d.ToString("f2")}%".Replace(".00%", "%");
}
You can use it like this.
var doubles = new double[] { 5.0, 5.999, 3.2 };
foreach (var d in doubles)
Console.WriteLine(MyDoubleToString(d));
and result will be
5%
5.99%
3.20%
If you want to use it in razor then
@MyDoubleToString(item.percentage)

Hamid Pourjam
- 20,441
- 9
- 58
- 74
-
That doesn't work with numbers like 5.999. It will return "6.00%" but I think it should return "6%" – Matthew Watson Dec 10 '15 at 09:08
-
fixed both the problem with `6.00` and the rounding problem @MatthewWatson – Hamid Pourjam Dec 10 '15 at 09:31
-
Hmm well now "5.99999" comes out as "5.99" which isn't correct, since (I think) it should be "6". The difference between 5.99999 and 5.99 is 0.00999, but the difference between 5.99999 and 6 is only 0.00001. However this will be OK if the OP says it is. :) – Matthew Watson Dec 10 '15 at 09:46
-
And what is the logic behind your reasoning? Why it should be rounded? 5.9999 percent is not equals to 6 percent unless we define a valid range for our precision. – Hamid Pourjam Dec 10 '15 at 09:48
-
My logic is that you want to minimise the rounding errors. If you round 5.99999 to two decimal places, it should be 6.00. That's what you'll get if you do `5.99999.ToString("f2")`. I understood the OP to want to do normal rounding, but if it was all zeroes at the end, to strip them off. – Matthew Watson Dec 10 '15 at 09:50
-
-
That works nicely if you comment out that line. You could also extend this to work with any number of decimal places by dynamically creating the format specifier and string to replace. At this point, our answers are essentially the same. :) – Matthew Watson Dec 10 '15 at 09:56
-
I think this is now a good answer (if you comment out the indicated line), so +1 – Matthew Watson Dec 10 '15 at 10:01
0
This is a bit hacky but it works...
public static string FormatDecimalWithPercent(decimal d, int decimalPlaces)
{
string format = "{0:f" + decimalPlaces + "}";
string candidate = string.Format(format, d);
string trimmed = candidate.TrimEnd('0');
if (trimmed.EndsWith("."))
return trimmed.TrimEnd('.') + "%";
return candidate + "%";
}
Here's a less hacky solution that works (and is therefore better):
public static string FormatDecimalWithPercent(decimal d, int decimalPlaces)
{
decimal rounded = decimal.Round(d, decimalPlaces);
decimal truncated = decimal.Round(d, 0);
if (rounded != truncated)
return string.Format("{0:f" + decimalPlaces + "}", rounded) + "%";
return truncated + "%";
}

Matthew Watson
- 104,400
- 10
- 158
- 276
-
@downvoter: It's useful to explain why you are downvoting, especially when the answer works! – Matthew Watson Dec 10 '15 at 09:41
-
I like this method a lot. Is it possible to turn in into not only an extension method but somewho also handle float, decimal, double in the same method ? like using Math.Round method ? But I cant see how we could generic constraint this. I guess we need dedicated three methods for it if no body got something we could use. However I guess we need to resort to reflection then.. – Tore Aurstad Jun 17 '22 at 16:06
0
@(item.percentage % 1==0 ? item.percentage.ToString("N0") : item.percentage.ToString("N2"))

tynar
- 136
- 8