double get fractional part?

  • Thread starter Thread starter Yin99
  • Start date Start date
Y

Yin99

If I have:

8.24
7.56
1.11

as doubles, how do I easily get just the ".24, .56, .11" ? Thanks,

Yin
 
If I have:

8.24
7.56
1.11

as doubles, how do I easily get just the ".24, .56, .11" ? Thanks,

Well, there's no shortage of methods I'm sure, but the following would work:

double value, fraction;

value = 8.24;
fraction = value - (int) value;

Pete
 
assuming the number is positive:

double x = myNumber-Math.Floor(myNumber);
 
I have a doubt : what occurs when <double> value exceeds <int> capacities ?
Loïc
 
I have a doubt : what occurs when <double> value exceeds <int>
capacities ?

Well, using the code you quoted you get an exception, of course.

If you have numbers that aren't within the range of int, you'll have to
use some other method obviously. For example, the Math.Truncate() method
can be used in place of casting to int.

Pete
 
Loïc Berthollet said:
I have a doubt : what occurs when <double> value exceeds <int> capacities ?

You should use:

fraction = value - (long) value;

or nicer:

fraction = value - Math.Floor(value);

to avoid the problem.

Arne
 
Peter said:
Well, using the code you quoted you get an exception, of course.

Only if compiled with /checked+.

Else the results will become very "weird looking".
If you have numbers that aren't within the range of int, you'll have to
use some other method obviously. For example, the Math.Truncate()
method can be used in place of casting to int.

That is probably better than my suggestion of Math.Floor - most
people will prefer -0.24 instead og 0.76 when applied to -8.24 !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top