Get the decimal part from a double

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

    You may remove the dot . from the double you are trying to get the decimals from using the Remove() function after converting the double to string so that you could do the operations required on it

    Consider having a double _Double of value of 0.66781, the following code will only show the numbers after the dot . which are 66781

    double _Double = 0.66781; //Declare a new double with a value of 0.66781
    string _Decimals = _Double.ToString().Remove(0, _Double.ToString().IndexOf(".") + 1); //Remove everything starting with index 0 and ending at the index of ([the dot .] + 1) 
    

    Another Solution

    You may use the class Path as well which performs operations on string instances in a cross-platform manner

    double _Double = 0.66781; //Declare a new double with a value of 0.66781
    string Output = Path.GetExtension(D.ToString()).Replace(".",""); //Get (the dot and the content after the last dot available and replace the dot with nothing) as a new string object Output
    //Do something
    

提交回复
热议问题