Created
April 18, 2014 09:50
-
-
Save FrankFan/11035002 to your computer and use it in GitHub Desktop.
C#加减乘除
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region 数值计算 | |
/// <summary> | |
/// 求2个数的百分比 | |
/// </summary> | |
/// <param name="num1"></param> | |
/// <param name="num2"></param> | |
/// <returns></returns> | |
public string GetRate(string num1, string num2) | |
{ | |
if (string.IsNullOrEmpty(num1) || string.IsNullOrEmpty(num2)) | |
return string.Empty; | |
string result = string.Empty; | |
double number1 = Convert.ToInt64(num1); | |
double number2 = Convert.ToInt64(num2); | |
double number3 = (number1 / number2) * 100.00; | |
result = string.Format("{0:F2}%", number3); | |
return result; | |
} | |
/// <summary> | |
/// 两个数相加 | |
/// </summary> | |
/// <param name="num1"></param> | |
/// <param name="num2"></param> | |
/// <returns></returns> | |
public string Add(string num1, string num2) | |
{ | |
if (string.IsNullOrEmpty(num1) || string.IsNullOrEmpty(num2)) | |
return string.Empty; | |
string result = string.Empty; | |
int number1 = Convert.ToInt32(num1); | |
int number2 = Convert.ToInt32(num2); | |
int number3 = number1 + number2; | |
result = number3.ToString(); | |
return result; | |
} | |
/// <summary> | |
/// 两个数相减 | |
/// </summary> | |
/// <param name="num1"></param> | |
/// <param name="num2"></param> | |
/// <returns></returns> | |
public string Subtraction(string num1, string num2) | |
{ | |
if (string.IsNullOrEmpty(num1) || string.IsNullOrEmpty(num2)) | |
return string.Empty; | |
string result = string.Empty; | |
int number1 = Convert.ToInt32(num1); | |
int number2 = Convert.ToInt32(num2); | |
int number3 = number1 - number2; | |
result = number3.ToString(); | |
return result; | |
} | |
/// <summary> | |
/// 两个数相除 | |
/// </summary> | |
/// <param name="num1"></param> | |
/// <param name="num2"></param> | |
/// <returns></returns> | |
public string Division(string num1, string num2) | |
{ | |
if (string.IsNullOrEmpty(num1) || string.IsNullOrEmpty(num2)) | |
return string.Empty; | |
string result = ""; | |
try | |
{ | |
int number1 = Convert.ToInt32(num1); | |
int number2 = Convert.ToInt32(num2); | |
double number3 = number1 * 1.00 / number2; | |
result = number3.ToString("F2"); | |
} | |
catch (Exception) | |
{ | |
} | |
return result; | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment