472,961 Members | 1,773 Online

# Need to remove the integer part and keep the fractional part of a double or decimal

I Pascal there are a function for retriving the fractional part of a real
number (Frac), I can't find any in the dotnet framework. The function needs
to remove the integer part and keep only the decimal part.

Any hints... I am hoping that I don't need to create a new class and method
for that function

/Michael Søndergaard
Nov 16 '05 #1
5 24181
Michael,

This is the same as "number modulo 1". e.g.:

public decimal Frac(decimal source)
{
return source % 1.0m;
}

HTH,
Nicole
"Michael Søndergaard" <sp**@spam.com> wrote in message
news:um**************@TK2MSFTNGP10.phx.gbl...
I Pascal there are a function for retriving the fractional part of a real
number (Frac), I can't find any in the dotnet framework. The function
needs
to remove the integer part and keep only the decimal part.

Any hints... I am hoping that I don't need to create a new class and
method
for that function

/Michael Søndergaard

Nov 16 '05 #2
Hi Michael,

Based on my understanding, you want to get the fractional part of a decimal.

In .Net, you should first convert any double or float number into Decimal
type. Because only Decimal type is accurate, double and float are both
approximate type.

Then, Decimal type has a Truncate static method, which will return the
interger part of a decimal, so you can divided it and get the fractional
part. Like this:

Decimal d=1.58M;
Decimal fractional=d-Decimal.Truncate(d);
Console.WriteLine(fractional.ToString());

===============================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #3
Jeffrey Tan[MSFT] <v-*****@online.microsoft.com> wrote:
Based on my understanding, you want to get the fractional part of a decimal.

In .Net, you should first convert any double or float number into Decimal
type. Because only Decimal type is accurate, double and float are both
approximate type.

I don't think that's a really fair characterisation. They're all
"inaccurate" if any of them are - it's just that many decimals aren't
exactly representable as binary floating point numbers. (Try
representing a third accurately in either of them, for instance...)

See http://www.pobox.com/~skeet/csharp/floatingpoint.html and
http://www.pobox.com/~skeet/csharp/decimal.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
Nov 16 '05 #4
Hi Jon,

Oh, yes, your statement is very precision. Just as you pointed out, Decimal
will remember how many decimal digits it has.

But, I think my code snippet should work for Michael.

Again, thanks for your contribution to the community!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #5
Hi Michael,

Does our reply make sense to you? Do you still have any concern on this
issue?

Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.