Get the decimal part from a double

后端 未结 18 846
佛祖请我去吃肉
佛祖请我去吃肉 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:36

    The simplest variant is possibly with Math.truncate()

    double value = 1.761
    double decPart = value - Math.truncate(value)
    
    0 讨论(0)
  • 2020-11-29 07:38
     string input = "0.55";
        var regex1 = new System.Text.RegularExpressions.Regex("(?<=[\\.])[0-9]+");
        if (regex1.IsMatch(input))
        {
            string dp= regex1.Match(input ).Value;
        }
    
    0 讨论(0)
  • 2020-11-29 07:42

    There is a cleaner and ways faster solution than the 'Math.Truncate' approach:

    double frac = value % 1;
    
    0 讨论(0)
  • 2020-11-29 07:43

    Better Way -

            double value = 10.567;
            int result = (int)((value - (int)value) * 100);
            Console.WriteLine(result);
    

    Output -

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

    Why not use int y = value.Split('.')[1];?

    The Split() function splits the value into separate content and the 1 is outputting the 2nd value after the .

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

    Use a regex: Regex.Match("\.(?\d+)") Someone correct me if I'm wrong here

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