Hi all,
I need to obtain the fractional part of a double. AFAIK C# and .NET has no such function.
Have a few options here:(x is a double)
x - (int)x
x - Math.Truncate(x)
Convert to a string and extract the fraction...
Perhaps someone knows other options?
Which should I choose or does it not matter that much?
Thanks in advance for some answers. :)

2 Years
Discussion Span
Last Post by JOSheaIV

I'd use Truncate or Floor because it returns a double. If you cast to an int you might get issues, because a double can contain much larger values.

As for converting to a string... You're doing math, stick with numbers ;)

Edited by pritaeas


Thanks for the advise. :)
I'll stick to Truncate, I tried Floor, but Floor gives 1 - fractional part for negative numbers.


I know it's been solved, but can I ask, would something like .ToString("N0"). That would return the number as a string, and chop off the decimal and everything past it (it won't handle rounding though, but I think some of the other number formats can do that, this explains it all Standard Numeric Format Strings)

Of course this is assume I read this right (the original post that is)

Edited by JOSheaIV


@AngelofD4rkness thanks for the tip, but as pritaeas pointed out in his first reply:"You're doing math, stick with numbers."


That is true (I usually like to work that way). I thought I read you wanted it as a string ... but I could have read that wrong

(Also, that whole rounding this is weird when it comes to C#, I forgot how I had to fix that once)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.