【问题标题】:Split double into two int, one int before decimal point and one after将 double 拆分为两个 int,一个 int 在小数点前,一个 int 在小数点后
【发布时间】:2011-12-23 00:27:48
【问题描述】:

我需要将一个 double 值拆分为两个 int 值,一个在小数点之前,一个在小数点之后。小数点后的 int 应该有两位数。

例子:

    10.50 = 10 and 50
    10.45 = 10 and 45
    10.5  = 10 and 50

【问题讨论】:

  • 提供了更多信息,抱歉不清楚。
  • 你真的需要它在两个整数中,还是你只需要作为文本的值,用逗号分隔它们?因为也许您应该使用基于文化的解决方案,而不是大多数人建议的 string.Split...
  • 是的,我需要两个不带逗号的整数。
  • 双打有最大精度吗?或任意小数位
  • 这通常是不可能的。小数点前的小数很容易超过 int 的数量(超过 300,但只有 15 个有效数字),小数点后的部分也是如此。

标签: c# double int decimal-point


【解决方案1】:

你可以这样做:

string s = inputValue.ToString("0.00", CultureInfo.InvariantCulture);
string[] parts = s.Split('.'); 
int i1 = int.Parse(parts[0]);
int i2 = int.Parse(parts[1]);

【讨论】:

  • 这样做将值转换为字符串不是最好的方法(它很慢),最好的选择是用数学来做,转换 integer 部分,然后将该值减去原始值decimal 获得其余的值。
【解决方案2】:

处理字符串可能很慢。尝试使用以下内容:

double number;

long intPart = (long) number;
double fractionalPart = number - intPart;

【讨论】:

  • 我认为这是最好的答案。
  • 它可能会有所帮助,但它回答了一个不同的问题。 fractionalPart0.xxxx...,而不是 xx
  • 请注意,这在某些情况下会导致精度问题。即 55.55 将拆分为 55 和 .549999999997。字符串转换会更昂贵,但有助于保持您的号码完整性。
【解决方案3】:

您想使用哪种编程语言来执行此操作?大多数语言应该有一个Modulo operator。 C++ 示例:

double num = 10.5;
int remainder = num % 1

【讨论】:

    【解决方案4】:
    "10.50".Split('.').Select(int.Parse);
    

    【讨论】:

      【解决方案5】:
      /// <summary>
      /// Get the integral and floating point portions of a Double
      /// as separate integer values, where the floating point value is 
      /// raised to the specified power of ten, given by 'places'.
      /// </summary>
      public static void Split(Double value, Int32 places, out Int32 left, out Int32 right)
      {
          left = (Int32)Math.Truncate(value);
          right = (Int32)((value - left) * Math.Pow(10, places));
      }
      
      public static void Split(Double value, out Int32 left, out Int32 right)
      {
          Split(value, 1, out left, out right);
      }
      

      用法:

      Int32 left, right;
      
      Split(10.50, out left, out right);
      // left == 10
      // right == 5
      
      Split(10.50, 2, out left, out right);
      // left == 10
      // right == 50
      
      Split(10.50, 5, out left, out right);
      // left == 10
      // right == 50000
      

      【讨论】:

        【解决方案6】:

        怎么样?

        var n = 1004.522
        var a = Math.Floor(n);
        var b = n - a;
        

        【讨论】:

          【解决方案7】:

          另一个不涉及字符串操作的变体:

          static void Main(string[] args)
          {
              decimal number = 10123.51m;
              int whole = (int)number;
              decimal precision = (number - whole) * 100;
          
              Console.WriteLine(number);
              Console.WriteLine(whole);
              Console.WriteLine("{0} and {1}",whole,(int) precision);
              Console.Read();
          }
          

          确保它们是小数,否则你会得到通常奇怪的浮点/双精度行为。

          【讨论】:

            【解决方案8】:

            你可以用字符串拆分,然后转换成int ...

            string s = input.ToString(); 
            string[] parts = s.Split('.');
            

            【讨论】:

              【解决方案9】:

              此函数需要十进制的时间并转换回以 60 为底的时间。

                  public string Time_In_Absolute(double time)
                  {
                      time = Math.Round(time, 2);
                      string[] timeparts = time.ToString().Split('.');                        
                      timeparts[1] = "." + timeparts[1];
                      double Minutes = double.Parse(timeparts[1]);            
                      Minutes = Math.Round(Minutes, 2);
                      Minutes = Minutes * (double)60;
                      return string.Format("{0:00}:{1:00}",timeparts[0],Minutes);
                      //return Hours.ToString() + ":" + Math.Round(Minutes,0).ToString(); 
                  }
              

              【讨论】:

                【解决方案10】:

                试试:

                string s = "10.5";
                string[] s1 = s.Split(new char[] { "." });
                string first = s1[0];
                string second = s1[1];
                

                【讨论】:

                  【解决方案11】:

                  您可以在不通过字符串的情况下做到这一点。示例:

                  foreach (double x in new double[]{10.45, 10.50, 10.999, -10.323, -10.326, 10}){
                      int i = (int)Math.Truncate(x);
                      int f = (int)Math.Round(100*Math.Abs(x-i));
                      if (f==100){ f=0; i+=(x<0)?-1:1; }
                      Console.WriteLine("("+i+", "+f+")");
                  }
                  

                  输出:

                  (10, 45)
                  (10, 50)
                  (11, 0)
                  (-10, 32)
                  (-10, 33)
                  (10, 0)
                  

                  不过,不适用于-0.123 这样的号码。再说一次,我不确定它是否适合您的代表。

                  【讨论】:

                    【解决方案12】:

                    我实际上只需要在现实世界中回答这个问题,而 @David Samuel 的回答在这里做了一部分,这是我使用的结果代码。如前所述,字符串开销太大。我必须对视频中的像素值进行此计算,并且仍然能够在中等计算机上保持 30fps。

                    double number = 4140 / 640; //result is 6.46875 for example
                    
                    int intPart = (int)number; //just convert to int, loose the dec.
                    int fractionalPart = (int)((position - intPart) * 1000); //rounding was not needed.
                    //this procedure will create two variables used to extract [iii*].[iii]* from iii*.iii*
                    

                    这用于从 640 X 480 视频源中的像素数求解 x,y。

                    【讨论】:

                      【解决方案13】:
                      Console.Write("Enter the amount of money: ");
                      double value = double.Parse(Console.ReadLine());
                      
                      int wholeDigits = (int) value;
                      double fractionalDigits = (value - wholeDigits) * 100;
                      fractionalDigits = (int) fractionalDigits;
                      
                      Console.WriteLine(
                          "The number of the shekels is {0}, and the number of the agurot is {1}",
                          wholeDigits, fractionalDigits);
                      

                      【讨论】:

                        【解决方案14】:

                        使用 Linq。只是澄清@Denis 的答案。

                        var parts = "10.50".Split('.').Select(int.Parse);
                        int i1 = parts.ElementAt(0);
                        int i2 = parts.ElementAt(1);
                        

                        【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多