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

Scott Parker picture Scott Parker · Nov 6, 2011 · Viewed 51.9k times · Source

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

Answer

Henk Holterman picture Henk Holterman · Nov 6, 2011

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]);