Get the decimal part from a double

后端 未结 18 847
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 06:47

I want to receive the number after the decimal dot in the form of an integer. For example, only 05 from 1.05 or from 2.50 only 50 not 0.50

相关标签:
18条回答
  • 2020-11-29 07:46
    var result = number.ToString().Split(System.Globalization.NumberDecimalSeparator)[2]
    

    Returns it as a string (but you can always cast that back to an int), and assumes the number does have a "." somewhere.

    0 讨论(0)
  • 2020-11-29 07:47

    Updated Answer

    Here I am giving 3 approaches for the same.

    [1] Math Solution using Math.Truncate

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

    // input : 1.05
    // output : "0.050000000000000044"

    // input : 10.2
    // output : 0.19999999999999929

    If this is not the result what you are expecting, then you have to change the result to the form which you want (but you might do some string manipulations again.)

    [2] 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

    If the number of decimal places is not fixed, then this approach may create problems.

    [3] using "Regular Expressions (REGEX)"

    we should be very careful while writing solutions with string. This would not be preferable except some cases.

    If you are going to do some string operations with decimal places, then this would be preferable

        string input_decimal_number = "1.50";
        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 : "1.05"
    // output : "05"

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

    // input : "0.0550"
    // output : "0550"

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

    0 讨论(0)
  • 2020-11-29 07:47

    It is very simple

           float moveWater =  Mathf.PingPong(theTime * speed, 100) * .015f;
           int    m = (int)(moveWater);
           float decimalPart= moveWater -m ;
    
           Debug.Log(decimalPart);
    
    0 讨论(0)
  • 2020-11-29 07:50

    Solution without rounding problem:

    double number = 10.20;
    var first2DecimalPlaces = (int)(((decimal)number % 1) * 100);
    Console.Write("{0:00}", first2DecimalPlaces);
    

    Outputs: 20

    Note if we did not cast to decimal, it would output 19.

    Also:

    • for 318.40 outputs: 40 (instead of 39)
    • for 47.612345 outputs: 61 (instead of 612345)
    • for 3.01 outputs: 01 (instead of 1)

    If you are working with financial numbers, for example if in this case you are trying to get the cents part of a transaction amount, always use the decimal data type.

    Update:

    The following will also work if processing it as a string (building on @SearchForKnowledge's answer).

    10.2d.ToString("0.00", CultureInfo.InvariantCulture).Split('.')[1]
    

    You can then use Int32.Parse to convert it to int.

    0 讨论(0)
  • 2020-11-29 07:51

    the best of the best way is:

    var floatNumber = 12.5523;
    
    var x = floatNumber - Math.Truncate(floatNumber);
    

    result you can convert however you like

    0 讨论(0)
  • 2020-11-29 07:51
    var decPlaces = (int)(((decimal)number % 1) * 100);
    

    This presumes your number only has two decimal places.

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