Take care of decimal gaps

L

Luigi

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.
 
J

Jon Skeet [C# MVP]

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
 
L

Luigi

Jon Skeet said:
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.


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
 
J

Jon Skeet [C# MVP]

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
 
H

Hans Kesting

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
 
B

Ben Voigt [C++ MVP]

Luigi said:
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<decimal> input;
List<decimal> output = new List(decimal);
decimal error = 0;
foreach (decimal x in input)
{
decimal y = Math.Round(x);
output.Add(y);
error += x - y;
}
 

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

Top