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. :)

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

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

Ah yes, didn't think of that.

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)

@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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.