Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对于较大和较小的符号,添加/删除到小数位_C#_Decimal_Data Conversion - Fatal编程技术网

C# 对于较大和较小的符号,添加/删除到小数位

C# 对于较大和较小的符号,添加/删除到小数位,c#,decimal,data-conversion,C#,Decimal,Data Conversion,我想将字符串值转换为十进制。当有一个更小或更大的符号时,我想像这样添加/删除该值 string value | decimal result --------------|---------------- "< 0.22" | 0.219 "< 32.45" | 32.449 "> 2.0" | 2.01 "> 5" | 5.1 字符串值|十进制结果 --------------|------------

我想将
字符串
值转换为
十进制
。当有一个更小或更大的符号时,我想像这样添加/删除该值

string value  |  decimal result
--------------|----------------
 "< 0.22"     |   0.219
 "< 32.45"    |   32.449
 "> 2.0"      |   2.01
 "> 5"        |   5.1
字符串值|十进制结果
--------------|----------------
"< 0.22"     |   0.219
"< 32.45"    |   32.449
"> 2.0"      |   2.01
"> 5"        |   5.1
这必须适用于任何小数位数的十进制数。有没有一种优雅的方法可以做到这一点


我只能考虑计算小数位数、获取最后一位数字、添加/删除…

所以我可以想象以下解决方案

  • 在空格上拆分字符串
  • 识别标志(GT或LT)
  • 计算小数位数并存储该值
  • 将数字转换为十进制
  • 根据符号加或减10^(-1*(numberOfDecimals+1))

  • 不计算小数的解决方案:

    如果字符串以
    开头,请附加
    “1”
    。如果字符串以
    “9”
    ,并在其左侧的字符上重复),然后追加
    “9”

    然后在空白处分开,<代码>双解析>代码>右部分。

    < p>不需要计算数字或拆分小数…(您可以在linqpad中运行此功能)

    使用正则表达式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] inputs = {
                    "< 0.22",
                    "< 32.45",
                     "> 2.0"
                                 };
                string pattern = @"(?'sign'[\<\>])\s+(?'integer'\d+).(?'fraction'\d+)";
    
                decimal number = 0;
                foreach(string input in inputs)
                {
                    Match match = Regex.Match(input, pattern);
                    if(match.Groups["sign"].Value == ">")
                    {
                       number = decimal.Parse(match.Groups["integer"].Value  + "." + match.Groups["fraction"].Value);
                       number += decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
                    }
                    else
                    {
                       number = decimal.Parse(match.Groups["integer"].Value  + "." + match.Groups["fraction"].Value);
                       number -= decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
                    }
    
                        Console.WriteLine(number.ToString());
                }
                Console.ReadLine();
    
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.Text.RegularExpressions;
    命名空间控制台应用程序1
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    字符串[]输入={
    "< 0.22",
    "< 32.45",
    "> 2.0"
    };
    字符串模式=@“(?'sign'[\])\s+(?'integer'\d+)(?'fraction'\d+);
    十进制数=0;
    foreach(输入中的字符串输入)
    {
    Match=Regex.Match(输入,模式);
    if(match.Groups[“sign”].Value==“>”)
    {
    number=decimal.Parse(match.Groups[“integer”].Value+“+match.Groups[“fraction”].Value);
    number+=decimal.Parse(“.”+新字符串('0',match.Groups[“fraction”].Value.Length)+“1”);
    }
    其他的
    {
    number=decimal.Parse(match.Groups[“integer”].Value+“+match.Groups[“fraction”].Value);
    number-=decimal.Parse(“.”+新字符串('0',match.Groups[“fraction”].Value.Length)+“1”);
    }
    Console.WriteLine(number.ToString());
    }
    Console.ReadLine();
    }
    }
    }
    
    这似乎是一个家庭作业/学习练习。你能展示一下你试过的吗?(提示:首先从不等式中分离出值,然后解析值。)家庭作业-你在开玩笑吗?小数位数有多少-这是否包括零(例如,
    32
    )?(我认为您计算小数位数的想法可能是最可靠和最简单的)。这似乎有点过分了,因为它是一个完全可运行的示例,所以代码太多了。干得好!至少编译这个正则表达式并重用它出于好奇,你为什么选择正则表达式作为公认的解决方案?我的解决方案不是更快而且代码更少吗?你是对的。但它解决了大于和小于的问题,所用的代码远少于正则表达式,速度也比正则表达式快,同时解决了您的问题“我只能考虑计算小数位数,获取最后一位数字,添加/删除…”。但是我为您更新了我的答案。这不起作用,因为您没有计算输入字符串中的小数位数,这可能类似于
    0.22000
    。事实上,任何工作解决方案都必须以某种方式计算输入字符串中的小数位数。我的意思是,如果输入是
    0.2200
    ,您的代码将生成
    0.219
    0.221
    ,但它应该是
    0.22001
    0.21999
    。另外,用OP的
    2.0
    的例子试试看……为什么?尾随的零是无关紧要的。如果他想要或不想要,代码就会工作。后面的零是重要的。它们表示输入数据的准确性。如果输入为
    1.0000
    ,则表示数据精确到小数点后4位,而如果输入为
    1.0
    ,则表示仅精确到小数点后1位-根据我的示例
    0.2200
    ,这两种情况下的预期输出不同。查看
    2.0
    的OP示例。并尝试与您的代码!好啊让我们走过去。"2.0"; 输入=2;gt=“2.0”+“1”=“2.01”=2.01;lt=2-(2.01-2)=1.99;哪一部分错了?我看是对的
    inputString = inputString.TrimEnd("0");
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] inputs = {
                    "< 0.22",
                    "< 32.45",
                     "> 2.0"
                                 };
                string pattern = @"(?'sign'[\<\>])\s+(?'integer'\d+).(?'fraction'\d+)";
    
                decimal number = 0;
                foreach(string input in inputs)
                {
                    Match match = Regex.Match(input, pattern);
                    if(match.Groups["sign"].Value == ">")
                    {
                       number = decimal.Parse(match.Groups["integer"].Value  + "." + match.Groups["fraction"].Value);
                       number += decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
                    }
                    else
                    {
                       number = decimal.Parse(match.Groups["integer"].Value  + "." + match.Groups["fraction"].Value);
                       number -= decimal.Parse("." + new string('0', match.Groups["fraction"].Value.Length) + "1");
                    }
    
                        Console.WriteLine(number.ToString());
                }
                Console.ReadLine();
    
            }
        }
    }