Search code examples
c#doubleintdecimal-point

Split double into two int, one int before decimal point and one after


I need to split an double value, into two int value, one before the decimal point and one after. The int after the decimal point should have two digits.

Example:

    10.50 = 10 and 50
    10.45 = 10 and 45
    10.5  = 10 and 50

Solution

  • This is how you could do it:

    string s = inputValue.ToString("0.00", CultureInfo.InvariantCulture);
    string[] parts = s.Split('.'); 
    int i1 = int.Parse(parts[0]);
    int i2 = int.Parse(parts[1]);