13

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?

easwee
  • 15,757
  • 24
  • 60
  • 83
Ricky
  • 171
  • 1
  • 2
  • 7

2 Answers2

26

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
  • 554,122
  • 78
  • 1,158
  • 1,373
2

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
  • 4,476
  • 9
  • 40
  • 50