Solved

Incorrect calculation.

Posted on 2012-03-27
9
229 Views
Last Modified: 2012-03-27
Dear Experts,

I have 1 button and 4 SpinEdit-components on my form. The default
values of the 4 spinedits are: 3,9 - 4,4 - 6,7 - 8,3 in the Settings.Settings-file.
and a boolean called IsMGDL with default value false.

With this button-event below a user can switch between 2 modes:
1. If IsMGDL = true then multiple the values of the spinedits with 18.
    And the outcome has to be changed to an integer-value.
2. If IsMGDL = false then devide the values of the spinedits with 18
    And the outcome has to be changed to a double-value.

        private void btnSwUnit_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.IsMGDL = !Properties.Settings.Default.IsMGDL;
            Properties.Settings.Default.Save();
            if (Properties.Settings.Default.IsMGDL)
            {
                btnSwUnit.Text = "Switch to mmol/l";
                lblSwUnit.Text = "Glucose Units: mg/dl";

                int intVal1 = Convert.ToInt32(spdMinVal.EditValue) * 18;
                spdMinVal.EditValue = intVal1.ToString();
                int intVal2 = Convert.ToInt32(spdMinRng.EditValue) * 18;
                spdMinRng.EditValue = intVal2.ToString();
                int intVal3 = Convert.ToInt32(spdMaxRng.EditValue) * 18;
                spdMaxRng.EditValue = intVal3.ToString();
                int intVal4 = Convert.ToInt32(spdMaxVal.EditValue) * 18;
                spdMaxVal.EditValue = intVal4.ToString();
            }
            else
            {
                btnSwUnit.Text = "Switch to mg/dl";
                lblSwUnit.Text = "Glucose Units: mmol/l";

                double doubleVal1 = Convert.ToDouble(spdMinVal.EditValue) / 18;
                spdMinVal.EditValue = doubleVal1.ToString();
                double doubleVal2 = Convert.ToDouble(spdMinRng.EditValue) / 18;
                spdMinRng.EditValue = doubleVal2.ToString();
                double doubleVal3 = Convert.ToDouble(spdMaxRng.EditValue) / 18;
                spdMaxRng.EditValue = doubleVal3.ToString();
                double doubleVal4 = Convert.ToDouble(spdMaxVal.EditValue) / 18;
                spdMaxVal.EditValue = doubleVal4.ToString();
            }
        }

Open in new window



But when I have the 3,9 - 4,4 - 6,7 - 8,3 in the spinedits and press the button
it gives an outcome of  72, 72,126, 144 while it should be: 70, 79, 121, 149.
I don't know why this his happening!

Who knows the answer and is willing to help me?

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
  • 5
  • 4
9 Comments
 
LVL 12

Accepted Solution

by:
mwochnick earned 500 total points
ID: 37772019
you need to round after the multiplication instead of before
4 * 18 is 72  where as 3,9 * 72 rounded is 70
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 37772025
so
int intVal1 = Convert.ToInt32(spdMinVal.EditValue *18);

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 37772078
I think you are right. I thank you for at least show me what the problem is.
Because I had no idea. But the spinedit is a component from devexpress.
so I get an error with your solution.

Operator '*' cannot be applied to operands of type 'object' and 'int'.

If you can solve it (because i don't know if its a devexpress related issue)
then tell me and I reward you the points.

Peter
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 12

Expert Comment

by:mwochnick
ID: 37772146
can you post the declaration for the spdMinVal control and the related using statement? or post the whole source file
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37772192
OKE.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37772235
This site doesn't allow to upload the complete zipfile so i have only
upload the Form1.cs.

I hope its enought...

Peter
Form1.cs
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37772359
Be back later.

I allready give you the points.
These where yours anyway...

Peter
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 37772382
ok try this
int intVal1 = Convert.ToInt32(spdMinVal.Value *18);

Open in new window

based on this documentation
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraEditorsSpinEdit_EditValuetopic
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37773167
Ah, thanks it works great.
Thank you again for all the help.

Greetings,

Peter Kiers
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question