简体   繁体   中英

converting Excel % to C#

in Excel if I have the following Values in row 1 cells A,B,C respectively

3.332125, 100%, 0% and for Cell $D$1 I have the formula : =($A$1*$B$1*($C$1+100%)+0.16)

This gives a value of 3.492125 which is what I want. I need to convert this to a C# formula and the problem I have is interpreting the 100%.

Can anyone offer the equivalent in C#

The Percentages must first be converted from strings. From This simmilar stack overflow question :

public decimal GetDecimalFromString(string valu)
{
    return decimal.Parse( valu.TrimEnd( new char[] { '%', ' ' } ) ) / 100M;
}

Essentially, num will be the Decimal value of, the string value, with the chars "%" and/or " " (space) Taken of the end, divided by 100. This will give your percentages as a decimal.

Then we can create a method to impliment your formula:

public decimal YourFormula(decimal A, decimal B, decimal C)
{
    return(A*B*(C+1)+0.16);
}

The 1 here is equal to 100%, as in excel

double a = 3.332125;
double b = 100/100;
double c = 0 / 100;
Console.WriteLine((a * b * (c + 100 / 100) + 0.16).ToString());

Result:

3.492125

Replace % with /100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM