c# - Double 数据类型,计算小数点后的小数位数

标签 c#

下面的方法应该返回“小数精度是多少,尾随数字的数量,这个( double )值有什么?”的答案。当值看起来像 5900.43、5900.043 等时,我做对了。当该方法收到 5900.00 时,它返回 0,这是错误的(在我的需要中)。

   /// <summary>
   /// Return count of decimals after decimal-place 
   /// </summary>
   private int getDecimalCount(double val)
   {
        int count = 0;
        ...
        return count;
   }

当参数 'val' 为(此方法的正常值样本)

5900.00 返回 2
5900.09 返回 2
5900.000 返回 3
5900.001 返回 3
1.0 返回 1
0.0000005 返回 7
1.0000000 返回 7
5900822 返回 0

我的问题是计算 .0 .00 .000 等等

问题:
我该如何解决这个问题?如果 double 无法使用此方法,还有什么办法?

[编辑]
- 对文本进行少量更改以提出更明确的问题,
- 由于错误使用“小数位”的含义而导致的语言更正

最佳答案

编辑:更新以简化对不同文化的处理。

与@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();
}

最后,测试的输出:

Testing with dVal = 5900 and sVal = 5900.00. Expected output: 2. Actual output: 2

Testing with dVal = 5900.09 and sVal = 5900.09. Expected output: 2. Actual output: 2

Testing with dVal = 5900 and sVal = 5900.000. Expected output: 3. Actual output: 3

Testing with dVal = 5900.001 and sVal = 5900.001. Expected output: 3. Actual output: 3

Testing with dVal = 1 and sVal = 1.0. Expected output: 1. Actual output: 1

Testing with dVal = 5E-07 and sVal = 0.0000005. Expected output: 7. Actual output: 7

Testing with dVal = 1 and sVal = 1.0000000. Expected output: 7. Actual output: 7

Testing with dVal = 5900822 and sVal = 5900822. Expected output: 0. Actual output: 0

如果您对此有任何疑问或它没有意义,请告诉我。

关于c# - Double 数据类型,计算小数点后的小数位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7346371/

相关文章:

c# - 在 ASP.NET Web API 中立即返回 HTTP 响应

c# - Visual Studio 'Publish' 命令失败

c# - 使用 LDAP/AD 设置静默身份验证

c# - WPF Bing map - 缩放至折线

c# - .NET Core 2 中的 ReadAsMultipartAsync 等价物

c# - 如何获取字符串值的最后一个索引

c# - 创建动态 Linq 到 EF 表达式以选择 IQueryable 到新类中并分配属性

c# - SQL Server 触发器 - 将消息发送到队列

C# 在 asp.net 中禁用 GridView header

c# - .Net Core 自定义身份验证使用 Identity Server 4 的 API key