Given X.Y
, I want to get X
and Y
.
For instance, given 123.456
I want to get 123
and 456
(NOT 0.456
).
I can do the following:
decimal num = 123.456M;
decimal integer = Math.Truncate(num);
decimal fractional = num - Math.Truncate(num);
// integer = 123
// fractional = 0.456 but I want 456
As above-mentioned, using this method I will get 0.456
, while I need 456
. Sure I can do the following:
int fractionalNums = (int)((num - Math.Truncate(num)) * 1000);
// fracionalNums = 456
Additionally, this method requires knowing how many fractional numbers a given decimal number has so that you can multiply to that number (e.g., 123.456
has three, 123.4567
has four, 123.456789
has six, 123.1234567890123456789
has nineteen).
Few points to consider:
- This operation will be executed millions of times; hence, performance is critical (maybe a bit-wise-based solution would do better);
- Precision is critical, and no rounding is acceptable.
NOTE 1
For performance reasons, I am NOT interested in string manipulation-based approaches.
NOTE 2
The numbers in my question are of decimal
type, hence methods that work for only decimal
types and fail on float
or double
(due to floating point precision) are acceptable.
NOTE 3
Two sides of decimal (i.e., integer and fractional parts) can be considered two integers. Hence, 123.000456
is not an expected input; and even if it is given, it is acceptable to split it to 123
and 456
(because both sides are to be considered integers).
X
andY
would be 3 and 6 for an input of either 3.6 or 3.00006. Thus,X
andY
fail to convey complete information about the input. Is that what you want?