Hi!!!
I have made a window application in which ,we have to roundoff the numbers of given expression to nearest 10,100,1000 and then solve the expression.So i have taken 3 textboxes.
In the first textbox User enter any expression
like 231*437.
In second user can enter 10 or 100 or 1000
And on button click the result should be displayed in third textbox ,which is 8000(i.e.if we enter 100 in second textbox then expression becomes200*400=80000 because nearest 100 of 231 is 200 and that of 437 is 400).


Can anyone give me C# code for this....

As far as I know the general rule on this forum is not to give full code, but rather to help you on your way. And It's my personal rule as well.

Anyway, I should be able to help out a bit. You're looking for the modulus operator, represented by %, which gives remainder after division. So 5%2 = 1.

The basic idea is to take each of your numbers, and use mod to take them to the required accuracy eg

//The desired accuracy
int acc
//The numbers from the text boxes
int a, b;
//The estimated numbers
int shortA, shortB;
//The remainder after division
int remA, remB;

//rid of the bits we don't want
remA = a - (a % acc);
shortA = a - remA;
//See if we should round up
if (remA >= (acc / 2))
{
    shortA =+ acc
} 
//Do the same for b

Something like that should do the trick. Then just do the multiplacation.

Hope that helps :)

M

Hi swinefish,
I know how to find round off numbers to nearest 10,100,1000...
But I donot know how to parse the expression which I am entering in first testbox such that it first find roundoff and then perform particular operation like multiply,divide etc...

Oh my bad

Still not sure I understand your problem (I get like this when I'm tired). But if I do understand you correctly, the String.Split method that ddanbe suggested is great. You could also use String.Substring to grab each of the 3 parts of the string in turn. It's probably not the most efficient way, but I find it very easy to understand.

M

@swinefish thanks for reminding me of the % operator:) This will do the job better then what I came up with, although I think your code snippet is not completely correct... remA = a - (a % acc); should read remA = a % acc; But as you where tired:yawn: I can understand, happens to me all the time.

Lol thanks for the pointer. Mixing two things there :) I shouldn't even be here, got work to do lol. But I like searching here for inspiration

As far as I know the general rule on this forum is not to give full code, but rather to help you on your way. And It's my personal rule as well.

Anyway, I should be able to help out a bit. You're looking for the modulus operator, represented by %, which gives remainder after division. So 5%2 = 1.

The basic idea is to take each of your numbers, and use mod to take them to the required accuracy eg

//The desired accuracy
int acc
//The numbers from the text boxes
int a, b;
//The estimated numbers
int shortA, shortB;
//The remainder after division
int remA, remB;

//rid of the bits we don't want
remA = a - (a % acc);
shortA = a - remA;
//See if we should round up
if (remA >= (acc / 2))
{
    shortA =+ acc
} 
//Do the same for b

Something like that should do the trick. Then just do the multiplacation.

Hope that helps :)

M

Hi ,
Your code works well for some numbers not for all.....
Like , if number entered is 89 and we have to roundoff to nearest 100 .In that case , the answer should be 100 but ur code is giving answer as 0 .....
what should be done for that case?

This article has been dead for over six months. Start a new discussion instead.