Quick technique for “truncating” the integer portion of a decimal value in C#, leaving only the fractional portion:
static decimal GetFractionalPortion(decimal d)
{
return d % 1;
}
Calling that method with the value 12.34 will return 0.34.
This works because the modulo operator (%) returns the remainder after dividing the first operand by the second, and any value divided by 1.0 returns the fractional portion of the value.
Note that if a negative value is used, the return value (the fractional portion) will also be negative.
No comments:
Post a Comment
Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!
Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!