Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal to percent or shift decimal places. How

Tags:

c#

decimal

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?

like image 455
Ricky Avatar asked Jun 09 '10 19:06

Ricky


People also ask

How do you shift decimal places?

If there IS a decimal point, move it to the right the same number of places that there are 0s. When dividing by 10, 100, 1000 and so on, move the decimal point to the left as many places as there are 0s. So when dividing by 10, move the decimal point one place, by 100 two places, by 1000 three places and so on.

Which movement of the decimal point is correct to convert a decimal to a percent?

To change a decimal to a percent, we multiply by 100. This is the same as moving the decimal point two places to the right. To change the decimal 0.22 to a percent, we move the decimal point two places to the right and get 22%.

What is 0.15 as a percentage?

Make use of the Free Decimal to Percent Calculator to change decimal value 0.15 to its equivalent percent value 0.0015% in no time along with the detailed steps.

Why do you move the decimal point 2 places percent?

Both mean that you have 52 parts out of 100. You move the decimal point two places to the right to show hundredths. Percent is “out of 100,” so the % sign is the same as two decimal places: 0.52 × 100 = 52 .


2 Answers

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.

like image 148
Reed Copsey Avatar answered Nov 15 '22 13:11

Reed Copsey


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 %
like image 31
Patrice Pezillier Avatar answered Nov 15 '22 13:11

Patrice Pezillier