The value after the decimal point!

  • Thread starter Thread starter RichW
  • Start date Start date
R

RichW

Hi,

I'm writing a program that has got a bit of maths in it. I perform a
division and then end up with something like 2.50, or 3.87, or 4.9975
(ie. y.12345).

I need to get rid of anything in front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,


Rich
 
Rich,

All you have to do is subtract .5 and then round the number to zero
decimal places. That will give you the integer portion of the number. You
can then just subtract that from the original number to get the decimal
portion. So, what you want is this:

// The number is in a variable named number, of type double.
// Get the integer portion.
double integerPortion = Math.Round(number - .5);

// Subtract from the original number.
double decimalPortion = number - integerPortion;

Hope this helps.
 
RichW said:
Hi,

I'm writing a program that has got a bit of maths in it. I perform a
division and then end up with something like 2.50, or 3.87, or 4.9975
(ie. y.12345).

I need to get rid of anything in front of the decimal point so that
I'm just left with the bit after the decimal point, ie. 0.50, 0.87,
0.9975, 0.12345 for the examples above.

I can't find the function/method I need to do this. I've spent a
considerable time looking and haven't got much more time to be able to
spend on this. I know this is a basic thing to want to do because I
remember it being on my calculator from my schooldays!

In desperation, I'm now considering converting the value (ie. 3.87) to
a string, and then doing a regex thing to remove anything before the
decimal point, adding a 0 to the start of the string, and then
converting it back to a number.

Please can someone suggest the best way to achieve what I need to do.
(I'm sure it must be because I'm overlooking something simple!).

Thanks very much in anticipation.

Best wishes,

Rich

double value = 1.234;
value -= (int)value;
 
Hi Rich,

This should be faster than string manipulation:

double real = 3.87;
double fraction = real - Math.Floor(real);

This works for positive numbers, so you may want to look at Math.Ceiling for
negative numbers.

Joe
 
Chrynne Schumptze said:
If you're using a float or double, if you cast it to an int, it will always
round down.

To be strictly accurate, I believe it always "rounds toward zero" (which
for negative numbers is rounding up). In other words, it always truncates
the decimal portion, which is what we want.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
Thank you all very much for your responses - they give me lots of ways to do it!

Thanks again for your help - I really appreciate it.

Best wishes,


Rich
 
Hey use this simple example,:thumb:

Decimal dcmNumber =128.8875;
Decimal dcmFstHalf = Math.Truncate(dcmNumber );//Now dcmFstHalf have 128
Decimal dcmSecHalf = dcmNumber - dcmFstHalf;//Now dcmSecHalf 0.8875

String str = Convert.ToString(dcmSecHalf); // now str got "0.8875"
try
{
str = str.Remove(0,2);//Now str have "8875"
}
catch
{ }

Int16 int16Final = Convert.ToInt16(str);//Convert your string result to integer or wahtever format u use
if (int16Final > 0)
{
//your condition here
}

Hope this will help, please let me know this was help full to you, :cheers:
 
Back
Top