473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Take care of decimal gaps

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
5 1275
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: John Bentley | last post by:
John Bentley: INTRO The phrase "decimal number" within a programming context is ambiguous. It could refer to the decimal datatype or the related but separate concept of a generic decimal number....
6
by: Sulsa | last post by:
Does memory allocated by opperator new has gaps, or is it one big block of memory??
5
by: cool2005 | last post by:
I have a table contains 4 cells (2 x 2) like this http://www.coolshare.com/planttrade/temp/gap.jpg each cell contains a div and the div contains a table. As you can see from the image above,...
4
by: Laphan | last post by:
Hi All Wonder if you could help, I have a bog standard table called STOCKPRICES that has served me well for a while, but now I need to change the structure of it and because a number of users...
3
by: Robert McGregor | last post by:
Hi there, I was wondering if anyone could help with this problem. I have a table with about 250,000 rows that relate to files that have been processed elsewhere in our business. Each file has...
2
by: pecan | last post by:
Okay, I give up. How do I do it? (Spent the last 4 hours searching for a solution) Here's the url: http://www.rouxville.info/rouxville-index.htm I can get this to display correctly in IE, but...
5
by: j.smith2c | last post by:
Hi everyone, I wanted to run the following loop, and enter y everytime (I expected that it will ask me to enter y for 5 time), but just after 3 times program terminates. what is the error?? ...
2
by: jaka | last post by:
Hi, I'm in need of some expert help in sorting a numerical/alphanumerical employee ID field (5&6 characters in length). Also seperating the alphanumerical IDs and finding the gaps in the...
13
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, Why does Math.Sqrt() only accept a double as a parameter? I would think it would be just as happy with a decimal (or int, or float, or ....). I can easily convert back and forth, but I am...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.