Solved

Incorrect calculation.

Posted on 2012-03-27
9
226 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now