Answered # rounding off

Discussion Starter Rajiv9 ss125 18 C#Jaap 5 ss125 18 ss125 18 # Here it is

# Now write the output as

Reverend Jim 1,421 C#Jaap 5 Discussion Starter Rajiv9 Discussion Starter Rajiv9 Begginnerdev 256 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

0

Hi,

I have a value like 2.90

i want the value to be round off to 3.00

can u provide me coding

Can u help me out

Rajiv

0

You can use

```
Math.Round(<%value%>, <%roundoff decimaltoints%>)
for eg,
Math.Round(6.42, 1)
```

Hope this helps u... :-D

0

Rounding off from 2.90 to 3.00 is very unusual. 2.90 is rounded to 3, for that you can use asl ss125 says Math.Round. You cannot use Math.Round(2.90, 2) to return 3.00, it will return 2.90!

0

fine...as far as i know generally we use the basic roundoff function. But considering your question,I want to answer my question.

What type of rounding do you want?

6.4 => 6

or

6.4 => 7

or

if 6.4=>6.4 but 6.9=>7

FOR WHICH ONE DO YOU NEED THE LOGIC?

0

Fine. I dont know whether VB.NET has any options to do with it.. But If you need a custom round function,I will tell you the logic to create your own round function.

1) Get the input in ther string format.

2) Using substring(),find the location of the [.] in the value.

3) now remove all the other number after the first digit of the decimal point using trim().

```
eg: 6.758698955 => 6.7
```

4) Store the value before [.] in a variable.

```
consider it as variable j
```

5) then get the value after the decimal point.

6) convert it into integer.

7) now in if condition you have to check two conditions.

7.1) if(i<5)

7.2) if(i>=5 and i!=9)

7.3) if(i=9)

8) Write appropriate code for each statements such as

```
if(1<5)
do nothing
```

ie., 6.4 => 6.4

```
if(i>=5 and i!=9)
do calculations based on what type do you want.if u don't need to do anything, skip this segment.
```

here is the important thing

```
if(i=9)
i=o
j=j+1
```

```
textbox1.text=i+"."+j
```

Hope this logic helps you my friend. :-D

0

You could try

```
Math.Truncate(yournum + 0.5)
```

and convert it to Int if you require an integer. I hate the way vb does rounding which is different for even numbers than for odd.

```
Math.Round(6.5) is 6.0
Math.Round(5.5) is 6.0
```

*Edited 3 Years Ago by Reverend Jim*

0

Try this

```
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
decimal pi = new decimal(3.14159265);
decimal piRoundedUp = Math.Round(pi, 4);
decimal piTruncated = Math.Round(pi, 4, MidpointRounding.AwayFromZero); //3.1416
// MidpointRounding.AwayFromZero is use
// to Round 2.5 to 3 where
// MidpointRounding.ToEven rounds to 2
// See http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx
Console.WriteLine("{0:0.0000}", pi); //normal round
Console.WriteLine("{0:0.0000}", piTruncated); //as above
// first multiply with 10^(number of decimals)
// and then devide with 10^(number of decimals)
// because Floor gets the integer part of the decimal.
decimal piFloored = Math.Floor(pi * (decimal)Math.Pow(10, 4)) / (decimal)Math.Pow(10, 4);
Console.WriteLine("{0:0.0000}", piFloored); //3.1415
Console.ReadKey();
}
}
}
```

*Edited 3 Years Ago by C#Jaap*

0

i will give u one example below :

amt = format(1000+2.6+3.5, "#,##0.00")

the above give me the result is 1,006.10 which iclude coma and two decimal place except round off

but if i will give this following formula :

amt = Round(1000+2.6+3.5, "0.00")

then the above give me the result is 1006 which is include Round off but not include coma and two decimal place

i want that formula which give Coma, two place decimal and Round off

0

i will give u one example below :

amt = format(1000+2.6+3.5, "#,##0.00")

the above give me the result is 1,006.10 which iclude coma and two decimal place except round off

but if i will give this following formula :

amt = Round(1000+2.6+3.5, "0.00")

then the above give me the result is 1006 which is include Round off but not include coma and two decimal place

i want that formula which give Coma, two place decimal and Round off like 1,006.00

0

Would something like this work:

```
FormatNumber(CInt(1000 + 2.6 + 3.5)), 2)
```

Just let the framework do the formatting for you.

*Edited 3 Years Ago by Begginnerdev*

This question has already been answered. Start a new discussion instead.

Recommended Articles

the function that I created to find the ...

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...