-1

I have the number 0.15 but I only want to show 15. How do I do that? *I need the solution to match every number (ex: 0.234 to 234, 0.3 to 3 etc...) Thanks for the helpers...

  • 3
    Possible duplicate of [Get the decimal part from a double](https://stackoverflow.com/questions/13038482/get-the-decimal-part-from-a-double) – Matt Nov 22 '18 at 15:50
  • 3
    Did you even try anything? There are plenty of similar questions around. Anyway: what if your number is greater 1? Do you want only the decimal part (as the duplicate assumes), or what else? – MakePeaceGreatAgain Nov 22 '18 at 15:51
  • 2
    Also, are you working with strings or numbers? Do you want your output as string or as a number? – Matt Nov 22 '18 at 15:55

1 Answers1

0

This way:

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);
    }
}
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32