Hi,
I want to round of the salary amount to the nearest 0.05. eg. if my salary is 1000.23, it should be rounded to 1000.25 and if the salary is 1000.26 it should be rounded off to 1000.30.
The Math.round function is not helpful in the above sceneario.
Suggestions pls
Regards
cmrhema

Recommended Answers

All 7 Replies

For the first case:
If your salary is 1000.23 it has to be 1000.25:
We multiply 1000.23 by 20, round it off to the nearest integer and divide it back by 20 :) ...

Edit:: In the second case (if your salary is 1000.26 and it has to rounded off to 1000.30): the Math.round function should work fine here !

Hope this helps !

If you want 1000.26 to round to 1000.30, you would have to use Math.ceil.

I hope this snippet does what you want :

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double d1 = 1000.23;
            double d2 = 1000.26;
           
            Console.WriteLine("d1= {0:F}", roundNum(d1,2));
            Console.WriteLine("d2= {0:F}", roundNum(d2,2));
            
            Console.ReadKey();
        }
     
        public static double roundNum(double num, int place)
        {
            double d = num * Math.Pow(10, place - 1);
            double floorValue = Math.Floor(d);
            if ((d - floorValue) > .5)
            {
                return (floorValue + 1) / Math.Pow(10, place - 1);
            }
            else
            {
                return (floorValue + 0.5) / Math.Pow(10, place - 1);
            }
        } 
    }
}

This does not work if place is smaller than 1, you have to test for it!

Hi !!!
I want to make window application In which I want to roundoff two numbers to nearest tens or hundreds or thousands....
After rounding off,then I want to find its sum \diff\mult\div...
On the form I have three textboxes i.e.one for entering the question,second for nearest roundoff(10,100,100) and another textbox for displaying final result.
For example-:
In first text box I enter 367*231
In the second I enter 100
Now in third textbox result should be 80000(i.e 400*200=80000 because nearest 100 of 367 is 400 and that of 231 is 200)

can anyone give me C# code for making this application...

This is code to round 231 to the nearest hundred :

int i = 231;
double d = i/100.0;
double r = Math.Round(d, 0);
 int roundInt = (int)r * 100;

Thanks for the code.......
this I already knew......
problem is that how will the computer recognise the expression in textbox1 i.e 367*231 and evaluate to solve the value

If you want to get help, post this question again in a new thread because this is a whole other issue then just rounding off.:-O
We will see you there:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.