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

  • Thread starter Thread starter Michael Søndergaard
  • Start date Start date
M

Michael Søndergaard

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
 
Michael,

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

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

HTH,
Nicole
 
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.
 
Jeffrey Tan said:
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

for more information.
 
Hi Jon,

Thanks for your correction.

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.
 
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.
 
Back
Top