views:

193

answers:

3

I have a that is generated from the diference of 2 numbers, but it return for exemple 0,07 for 7% and 0,5 for 50% i just want to fix to reach these goar, like 15,2% 13% and so on. How can I do that? do c# has something embeded on CLR to do that?

A: 

Just multiply it with 100.

ho1
This works, but it's nicer to use the built-in format option...
Reed Copsey
+6  A: 

You can use the Percentage Custom Numeric Format String. This will do it without you having to multiply yourself.

For example, you can do:

double value = 0.152;
string result = value.ToString("#0.##%");

If your locale is set to European (my guess based off the format you wrote), you'll get a string with the value of "15,2%".

You can also just use the Standard Percentage ("P") Format String, if you're happy with the default formatting.

Reed Copsey
I agree that this is the better answer, I had forgotten about those.
ho1
+1  A: 

Take a look there : Custom Numeric Format Strings

 double number = .2468013;
Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
// Displays 24.68 %
Console.WriteLine(number.ToString("P", 
                  CultureInfo.CreateSpecificCulture("hr-HR")));
// Displays 24,68%     
Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
// Displays 24.7 %
Patrice Pezillier