Ricky
Ricky

Reputation: 171

Convert decimal to percent or shift decimal places. How

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?

Upvotes: 13

Views: 36660

Answers (2)

Reed Copsey
Reed Copsey

Reputation: 564373

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.

Upvotes: 28

Patrice Pezillier
Patrice Pezillier

Reputation: 4566

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 %

Upvotes: 2

Related Questions