Solved

Incorrect calculation.

Posted on 2012-03-27
9
227 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 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

21 Experts available now in Live!

Get 1:1 Help Now