如何只显示一部分数字

时间:2018-11-22 15:47:19

标签: c# helper

我的数字是0.15,但我只想显示15。 我怎么做? *我需要解决方案以匹配每个数字(例如:0.234到234、0.3到3等) 谢谢你的帮手...

1 个答案:

答案 0 :(得分:0)

这种方式:

class Program
{        
    static void Main(string[] args)
    {
        double number = 1.234;
        number = GetDecimalPart(number);

        float number1 = 1.234f;
        number1 = GetDecimalPart(number1);

        decimal number2 = 1.234m;
        number2 = GetDecimalPart(number2);
    }

    private static double GetDecimalPart(double number)
    {
        string strNumber = number.ToString(CultureInfo.InvariantCulture);
        strNumber = strNumber.Substring(strNumber.IndexOf(".") + 1);
        return double.Parse(strNumber);
    }

    private static float GetDecimalPart(float number)
    {
        string strNumber = number.ToString(CultureInfo.InvariantCulture);
        strNumber = strNumber.Substring(strNumber.IndexOf(".") + 1);
        return float.Parse(strNumber);
    }

    private static decimal GetDecimalPart(decimal number)
    {
        string strNumber = number.ToString(CultureInfo.InvariantCulture);
        strNumber = strNumber.Substring(strNumber.IndexOf(".") + 1);
        return decimal.Parse(strNumber);
    }
}