By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

Take care of decimal gaps

P: n/a
Hi all,
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have

decimal a = 10.50m

should became 10

and if

decimal b = 10.51m

should became 11.

Then I need to populate a decimal variable with these rest with the sing
values.
So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

In this example

decimal c = + 0.50 - 0.49 // It will be 0.01

How can I accomplish this?

Thanks in advance.
--
Luigi

Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jun 18, 11:16*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have

decimal a = 10.50m
should became 10
and if
decimal b = 10.51m
should became 11.
Have a look at Math.Round(decimal, MidpointRounding)

Mind you, if you want 9.50m to round down to 9 instead of up to 10, I
don't think you'll find a version of MidpointRounding that does what
you want.
Then I need to populate a decimal variable with these rest with the sing
values.
So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

In this example
decimal c = + 0.50 - 0.49 // It will be 0.01
How can I accomplish this?
That already works. It's not clear to me what you're trying to do
here.

Jon
Jun 27 '08 #2

P: n/a
"Jon Skeet [C# MVP]" wrote:
On Jun 18, 11:16 am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have

decimal a = 10.50m
should became 10
and if
decimal b = 10.51m
should became 11.

Have a look at Math.Round(decimal, MidpointRounding)

Mind you, if you want 9.50m to round down to 9 instead of up to 10, I
don't think you'll find a version of MidpointRounding that does what
you want.
Then I need to populate a decimal variable with these rest with the sing
values.
So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

In this example
decimal c = + 0.50 - 0.49 // It will be 0.01
How can I accomplish this?

That already works. It's not clear to me what you're trying to do
here.
Hi Jon,
thanks for reply.
Mi task is to "not lose" the decimal rounding of these values.
Ok for the Math.Round it works fine for me, but I've not found a solution to
recover the decimal values (they are monetary issues).

Luigi

Jun 27 '08 #3

P: n/a
On Jun 18, 11:57*am, Luigi <ciupazNoSpamGra...@inwind.itwrote:
Mi task is to "not lose" the decimal rounding of these values.
It's not clear to me exactly what you mean.
Ok for the Math.Round it works fine for me, but I've not found a solutionto
recover the decimal values (they are monetary issues).
Recover them from where? What's losing them? Where are you actually
having problems?

Jon
Jun 27 '08 #4

P: n/a
Luigi has brought this to us :
Hi all,
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have

decimal a = 10.50m

should became 10

and if

decimal b = 10.51m

should became 11.

Then I need to populate a decimal variable with these rest with the sing
values.
So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

In this example

decimal c = + 0.50 - 0.49 // It will be 0.01

How can I accomplish this?

Thanks in advance.
I'm guessing that you want to round a monetary value to whole (say)
euro's and keep the "rounding offset"?

What about:

decimal originalAmount = 10.50m;
decimal wholeEuros = Math.Round(originalAmount);
decimal centsFraction = originalAmount - wholeEuros;

Hans Kesting
Jun 27 '08 #5

P: n/a
Luigi wrote:
Hi all,
I have a collection (an array for example) of decimal values.
I need to round them and take care of the roundings.
For example, if I have

decimal a = 10.50m

should became 10

and if

decimal b = 10.51m

should became 11.

Then I need to populate a decimal variable with these rest with the
sing values.
So 0.50 it will have the "+" sign, and the 0.49 will have "-" sign.

In this example

decimal c = + 0.50 - 0.49 // It will be 0.01

How can I accomplish this?
IEnumerable<decimalinput;
List<decimaloutput = new List(decimal);
decimal error = 0;
foreach (decimal x in input)
{
decimal y = Math.Round(x);
output.Add(y);
error += x - y;
}
>
Thanks in advance.

Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.