Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Incorrect calculation.

Posted on 2012-03-27
9
Medium Priority
?
236 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 2000 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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview

824 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