C# get digits from float variable

前端 未结 12 1638
南笙
南笙 2020-12-06 16:26

I have a float variable and would like to get only the part after the comma, so if I have 3.14. I would like to get 14 as an integer. How can I do that?

相关标签:
12条回答
  • 2020-12-06 17:11

    To suggest something different than the others, an extension method (with a method similar to David's):

    public static int GetDecimalAsInt(this float num)
    {
        string s = n.ToString();
        int separator = s.IndexOf(System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.NumberDecimalSeparator);
        return int.Parse(s.Substring(separator + 1));
    }
    
    // Usage:
    float pi = 3.14;
    int digits = pi.GetDecimalAsInt();
    

    Edit: I didn't use the "best" answer, because it omitted the hardest part, which is converting an arbitrary decimal number, and did not work for negative numbers. I added the correction requested in David's answer.

    0 讨论(0)
  • 2020-12-06 17:15
    float x = 3.14
    int fractionalPortionAsInt = (int) (100 * (x - Math.Floor(x)));
    
    0 讨论(0)
  • 2020-12-06 17:17

    Just in case some wants another cheating way for it:

    float x = 5.2f;
    int decimalPart = Math.Round((x - Math.Truncate(x))*100)
    

    where 100 is used shift the decimal part.

    0 讨论(0)
  • 2020-12-06 17:18

    You can subtract the integer portion from the value itself to retrieve the fractional part.

    float x = 3.14
    float fractionalPortion = x - Math.Truncate(x);
    

    You can then multiply it to get the fractional part represented as an integer at whatever precision you'd like.

    Mapping the fractional portion to an integer has some challenges - many floating point numbers cannot be represented as a base-10 integer, and thus may require more digits to represent than an integer can support.

    Also, what of the case of numbers like 3.1 and 3.01? Mapping directly to an integer would both result in 1.

    0 讨论(0)
  • 2020-12-06 17:19

    Here's another version that also tells how many digits are part of the fractional make-up, which I needed.

    public static int GetFractionalPartAsInt(decimal n, out int numOfFractionalDigits)
    {
      n -= Math.Truncate(n);
      n = Math.Abs(n);
    
      int numOfFractionalDigitsValue = 0;
      // When n != Math.Truncate(n), we have seen all fractional decimals.
      while (n != Math.Truncate(n))
      {
        n *= 10;
        numOfFractionalDigitsValue++;
      }
    
      numOfFractionalDigits = numOfFractionalDigitsValue;
    
      return (int)n;
    }
    

    It's similar in idea to David's answer (his non-cheating version). However, I used the decimal type instead of double, which slows things down, but improves accuracy. If I convert David's (again, non-cheating version) answer to use a decimal type (in which case his "precision" variable can be changed to the constant zero), my answer runs about 25% faster. Note that I also changed his code to provide the number of fractional digits in my testing.

    0 讨论(0)
  • 2020-12-06 17:22

    using Regular Expressions (REGEX)

    string input_decimal_number = "3.14";
    var regex = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
    if (regex.IsMatch(input_decimal_number))
    {
        string decimal_places = regex.Match(input_decimal_number).Value;
    }
    

    // input : "3.14"
    // output : "14"

    // input : "2.50"
    // output : "50"

    you can find more about Regex on http://www.regexr.com/

    Math Solution using Math.Truncate

     var float_number = 12.345;
     var result = float_number - Math.Truncate(float_number);
    

    Using multiplier [which is 10 to the power of N (e.g. 10² or 10³) where N is the number of decimal places]

       // multiplier is " 10 to the power of 'N'" where 'N' is the number 
       // of decimal places
       int multiplier = 1000;  
       double double_value = 12.345;
       int double_result = (int)((double_value - (int)double_value) * multiplier);
    

    // output 345

    0 讨论(0)
提交回复
热议问题