float f = 0.479f;
Console.WriteLine(f.ToString("p1"));
The output: 47.9 %
What should I pass to ToString() in order to remove the percentage sign for output like this:
47.9
EDIT. I should have mentioned that I am passing the mask to a 3rd party component that does it's thing with it. I can't perform any acrobatics with the numbers unfortunately. It has to be the mask that does the trick.
Click the Number tab on the Format Cells window. Click the "General" listing in the Category pane. This action will remove the percentage signs and display the number without any formatting.
Percentages are calculated by using the equation amount / total = percentage. For example, if a cell contains the formula =10/100, the result of that calculation is 0.1. If you then format 0.1 as a percentage, the number will be correctly displayed as 10%.
#1 select those numbers in new column, and then right click on it, and select Format Cells from the popup menu list. And the Format Cells dialog will open. #2 click Custom under Category list box, and type in “0\%” in Type text box, and then click OK button.
I should have mentioned that I am passing the mask to a 3rd party component that does it's thing with it. I can't perform any acrobatics with the numbers unfortunately. It has to be the mask that does the trick.
So I assume that you are stuck with Float.ToString(String)
, and that you cant only edit the p1
in f.ToString("p1")
. Now that's a tricky one. If you're not afraid of breaking anything related to the changes implied, you could do the following:
The "P" numeric format, as documented On MSDN, uses NumericFormatInfo.PercentSymbol
in order to write the "%" sign. NumericFormatInfo
is a member of your current CultureInfo
. What you could do, is clone your CurrentCulture
, and modify the PercentSymbol to ""
like the following:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
float f = 0.479f;
Console.WriteLine(f.ToString("p1")); //will write 47.9%
CultureInfo ci = CultureInfo.CurrentCulture.Clone() as CultureInfo;
ci.NumberFormat.PercentSymbol = "";
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
Console.WriteLine(f.ToString("p1")); //will write 47.9
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
This would be the way to go if you don't want to alter the ToString call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With