Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i need to round up "numbers"
like so: (123.6 = 124)

this is a programm that calculates the difference between average and the random numbers in the array.

C#
{

            int[] Elements = new int[20]; // this creates/declares an integer array with 20 Elements

            double avg = 0, sum = 0, diff = 0; // declare average variable

            Random rn = new Random();

            for (int i = 0; i < Elements.Length; i++) // this is a loop to show the random numbers of elements
            {
                int Element = i; // declare the Element variable

                int numbers = rn.Next(0, 200); // creates a number between 0 and 200




                double doubleValue = decimal.ToDouble(numbers);

                Math.Round(doubleValue,0, MidpointRounding.AwayFromZero);


                Console.WriteLine("Element {0} is {1}", Element, doubleValue); // print values

                Elements[Element] = numbers;




                sum += doubleValue; // calculate the sum of numbers
                avg = sum / Elements.Length; // calculate average of the sum                            
            }

            Console.WriteLine("\n");
            Console.WriteLine("The average is: {0:F}", avg);
            Console.WriteLine("\n");

            for (int z = 0; z < Elements.Length; z++)
            {
                diff = Elements[z] - avg;
                Console.WriteLine("Difference between average and element {0} is {1:N2}", z, Math.Abs(diff));
            }
            Console.ReadKey();

        }
        }


What I have tried:

C#
double doubleValue = decimal.ToDouble(numbers);

Math.Round(doubleValue,0, MidpointRounding.AwayFromZero);
Posted
Updated 22-Oct-21 22:24pm
v3

There is no standard method which will round up all numbers identically:
123.99  -> 124
124.00  -> 124
124.01  -> 125
The Math.Round method will round to the nearest integer, or to an odd or even number - but it won't round the way you want it to.
You would have to write your own method which checks the fractional part of the value and applies the rounding for you.

But ... that's some odd code.
C#
int numbers = rn.Next(0, 200); // creates a number between 0 and 200
double doubleValue = decimal.ToDouble(numbers);
Every value will be a "nnn.00" value because the numbers value is an integer. And why are you using a decimal conversion to change an int to a double?
I think that getting the rounding right is the least of your problems here ... :laugh:
 
Share this answer
 
Comments
PIEBALDconsult 22-Oct-21 16:40pm    
"round up" is a non-sensical term.
Either you want to round, truncate, or ceiling.
Richard Deeming 25-Oct-21 6:55am    
Also, the comment is wrong: rn.Next(0, 200) will return a number between 0 and 199, not 200. :)
There are too many obvious errors in this code for me to be more helpful, but ...

You need to think about the number of decimal-places possible for C# floating-point Numeric Types: do the Types you use allow 10-decimal place values ? Start here: [^].

Since there is no C# "random get next float" function (Java has one), you need to evaluate what Random.NextDouble does. If you want random C# Type float values, see this excellent StackOverFlow Wiki: [^]. You need random values including negative values: [^]

Remember that you need a very large sample of values for average, median, and mean, values to "mean" something statistically significant.
 
Share this answer
 
Quote:
How do I round up my array values by tenth decimal place?

Rather vague description of your rounding, and your only example is normal rounding (to closest integer).
Here is a link to python roundings: How to Round Numbers in Python – Real Python[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900