编辑:更新以简化不同文化的处理。
与@Miguel 类似,这就是我的处理方式:
public static int getDecimalCount(double dVal, string sVal, string culture)
{
CultureInfo info = CultureInfo.GetCultureInfo(culture);
//Get the double value of the string representation, keeping culture in mind
double test = Convert.ToDouble(sVal, info);
//Get the decimal separator the specified culture
char[] sep = info.NumberFormat.NumberDecimalSeparator.ToCharArray();
//Check to see if the culture-adjusted string is equal to the double
if (dVal != test)
{
//The string conversion isn't correct, so throw an exception
throw new System.ArgumentException("dVal doesn't equal sVal for the specified culture");
}
//Split the string on the separator
string[] segments = sVal.Split(sep);
switch (segments.Length)
{
//Only one segment, so there was not fractional value - return 0
case 1:
return 0;
//Two segments, so return the length of the second segment
case 2:
return segments[1].Length;
//More than two segments means it's invalid, so throw an exception
default:
throw new Exception("Something bad happened!");
}
}
还有一个美式英语的捷径:
public static int getDecimalCount(double dVal, string sVal)
{
return getDecimalCount(dVal, sVal, "en-US");
}
测试:
static void Main(string[] args)
{
int i = 0;
double d = 5900.00;
string s = "5900.00";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 2. Actual output: {0}", i);
Console.WriteLine();
d = 5900.09;
s = "5900.09";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 2. Actual output: {0}", i);
Console.WriteLine();
d = 5900.000;
s = "5900.000";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 3. Actual output: {0}", i);
Console.WriteLine();
d = 5900.001;
s = "5900.001";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 3. Actual output: {0}", i);
Console.WriteLine();
d = 1.0;
s = "1.0";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 1. Actual output: {0}", i);
Console.WriteLine();
d = 0.0000005;
s = "0.0000005";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 7. Actual output: {0}", i);
Console.WriteLine();
d = 1.0000000;
s = "1.0000000";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 7. Actual output: {0}", i);
Console.WriteLine();
d = 5900822;
s = "5900822";
Console.WriteLine("Testing with dVal = {0} and sVal = {1}.", d, s);
i = getDecimalCount(d, s);
Console.WriteLine("Expected output: 0. Actual output: {0}", i);
Console.ReadLine();
}
最后是测试的输出:
使用 dVal = 5900 和 sVal = 5900.00 进行测试。
预期输出:2。实际输出:2
使用 dVal = 5900.09 和 sVal = 5900.09 进行测试。
预期输出:2。实际输出:2
使用 dVal = 5900 和 sVal = 5900.000 进行测试。
预期输出:3。实际输出:3
使用 dVal = 5900.001 和 sVal = 5900.001 进行测试。
预期输出:3。实际输出:3
使用 dVal = 1 和 sVal = 1.0 进行测试。
预期输出:1。实际输出:1
使用 dVal = 5E-07 和 sVal = 0.0000005 进行测试。
预期输出:7。实际输出:7
使用 dVal = 1 和 sVal = 1.0000000 进行测试。
预期输出:7。实际输出:7
使用 dVal = 5900822 和 sVal = 5900822 进行测试。
预期输出:0。实际输出:0
如果您对此有任何疑问或没有意义,请告诉我。