Solved

Incorrect calculation.

Posted on 2012-03-27
9
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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