-1

I have a question. In my case I want to retrieve the last digit of a decimal value in order to do rounding adjustment. Following is my code:

private void calculateADJ()
    {
        string result = Strings.Right(txtGTotal.Text, 1);
        if (result == "1" || result == "6")
        {
            txtStoreRA.Text = "-0.01";
            txtOutstanding.Text = (Convert.ToDouble(txtGTotal.Text) - 0.01).ToString("0.00");
        }

        if (result == "2" || result == "7")
        {
            txtStoreRA.Text = "-0.02";
            txtOutstanding.Text = (Convert.ToDouble(txtGTotal.Text) - 0.02).ToString("0.00");
        }
        if (result == "3" || result == "8")
        {
            txtStoreRA.Text = "0.01";
            txtOutstanding.Text = (Convert.ToDouble(txtGTotal.Text) + 0.01).ToString("0.00");
        }

        if (result == "4" || result == "9")
        {
            txtStoreRA.Text = "0.02";
            txtOutstanding.Text = (Convert.ToDouble(txtGTotal.Text) + 0.02).ToString("0.00");
        }
        else
        {
            txtOutstanding.Text = txtGTotal.Text;
        }
    }

For example, if the number is 11.93, it will convert it into 11.95. Any suggestion or help will be much appreciate. For your information, I'm doing this with pocket PC emulator 2003 in Visual studio 2005.

My problem is that the value didn't change as I expected.

When I run my program, for example the txtGTotal.Text = 11.94, so my txtOutStanding.Text should be 11.95. However, txtOutStanding.Text still 11.94.

Not sure who is the one who devoting, at least you can provide me the explanation. Getting devote without a reason feels suck, a reason will at least can be a improvement for next time before asking a question. Thank you

I would say Ian's answer is much clearer than the answer provided at the post that I duplicate.

TheButterfly
  • 85
  • 1
  • 3
  • 20
  • Wouldn't Math.Round be a better option? https://msdn.microsoft.com/en-us/library/f5898377(v=vs.110).aspx –  Jan 19 '16 at 09:11
  • Where exactly are you having problems? – Lars Kristensen Jan 19 '16 at 09:12
  • @AndyJ isn't Math.Round just convert into specific decimal places? Mine will be to get the last value and do the calculation. It is not the same as Math.Round. – TheButterfly Jan 19 '16 at 09:13
  • Hmm, this seems an [X-Y question](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). *Why* are you doing such a strange rounding? – Matthew Watson Jan 19 '16 at 09:13
  • @LarsKristensen sorry I didn't mention it in my question, will update now. My problem is, the textbox didn't change value as I expected – TheButterfly Jan 19 '16 at 09:13
  • What did you input and what was the output or what was the error that you received, please provide all details – novice Jan 19 '16 at 09:14
  • @MatthewWatson I want to make the money calculation be easier so end user will not paying like 11.11 or 11.17 but can round off to 11.10 or 11.15. Something like that – TheButterfly Jan 19 '16 at 09:16
  • @MXD looks possible, my bad didn't Google probably. Thanks for the link and I will check it out – TheButterfly Jan 19 '16 at 09:17
  • @novice I will update into my question. Thanks for the suggestion – TheButterfly Jan 19 '16 at 09:18
  • @HenkHolterman human error, was thinking and typing. Thanks for reminding – TheButterfly Jan 19 '16 at 09:23
  • So you want to `round to the nearest 0.05`? – Matthew Watson Jan 19 '16 at 09:27
  • @MatthewWatson for example, if it is 11.11, it will round to 11.10, if 11.19, it will round to 11.20 – TheButterfly Jan 19 '16 at 09:29
  • @HenkHolterman I read the dupe, notice that it has a well received answer which uses Math.Ceiling instead of Math.Round, for the nearest integer that the OP means is to round up only. Would then this case be considered a dupe? (I am not sure about this site standard of dupe, since I have joined but less than 40 days here. So I am simply asking to get an idea, nothing more or less) – Ian Jan 19 '16 at 14:49
  • @TheButterfly I checked the dupe for clarification. Though the title really suggest a dupe, I found that the actually question the OP there is really asking is quite different - for he rather asks for round up than nearest integer (round up or down). Check out his example: Ex: 7.125 -> 7.15 6.66 -> 6.7 both are round up. – Ian Jan 19 '16 at 14:53
  • @Ian i don't really mind they mark my question as duplicate as long as it will really help someone out. – TheButterfly Jan 20 '16 at 01:37
  • @TheButterfly I get it (that's a good response!). :) I mean just in case if you wonder why it is considered dupe, perhaps it is because of the similar nature of the question (albeit having slight difference). – Ian Jan 20 '16 at 01:40
  • @Ian we are thinking the same thing. However, i still insist your answer is much clearer :v – TheButterfly Jan 20 '16 at 01:53

1 Answers1

6

Because your lowest unit is (0.05, which is 1/20th of smallest int 1), the simplest way would be to multiply you value by 20 first before you use Math.Round for the rounding, and then revert it back as needed.

Or, more simply, you could define this 0.05 as your lowestUnit

For example, suppose you have:

txtGTotal.Text = "11.93";    
double val = double.Parse(txtGTotal.Text);

Do this:

double myLowestUnit = 0.05;
double result = myLowestUnit * Math.Round(val/myLowestUnit); //this will give you 11.95
txtOutStanding.Text = result.ToString("f2"); //use f2 to limit the number of string printed to two decimal places

And to get val from the TextBox, simply use double.Parse(txtGTotal.Text);

Note that you could use string.ToString("f2") to print the value in the format which you want in your txtOutStanding TextBox.

Ian
  • 30,182
  • 19
  • 69
  • 107