Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:35:55+00:00 2026-06-18T17:35:55+00:00

I have this switch statement that decreases the double itemCost (originally 0.80) by 0.20

  • 0

I have this switch statement that decreases the double itemCost (originally 0.80) by 0.20 each time. When it reaches 0.00 i want to execute some code. It works fine until it gets to 0.00 but instead of being 0.00 itemCost becomes 5.55111512312578E-17.

Output values for itemCost as I press the button which executes switch statement are:

0.60, 0.40, 0.20, 5.55111512312578E-17, -0.20, -0.40 etc

Code:

switch (codeString)
{
    case "20":
    {
        userAmount = userAmount + 0.20;
        itemCost = itemCost - 0.20;
        Console.WriteLine("" + itemCost);
        if (itemCost == 0.00)
        {
            giveChange();
            labelInstructions.Text = "giveChange";
        }
        else
        {
            string tempString = string.Format("{0:N2}", itemCost);
            labelInstructions.Text = "Please insert £" + tempString;
        }
        break;
    }
}

Anyone know why this weird behavior occurs and how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:35:56+00:00Added an answer on June 18, 2026 at 5:35 pm

    Since you are dealing with currency, you should switch over and use the Decimal datatype.

    Floats and Doubles encounter very, very strange behavior when dealing with addition and subtraction as you get closer to 0.0.

    It is critical when programming on all platforms to be very wary of floating point math when you need absolute precision. For example, game developers are
    forced to use floating-points, due to graphics-card, graphics-library, and performance restrictions. Typically, when they want to see if a value is zero, instead of comparing against zero, they do the following (pseudo code)

    if AbsoluteValue(MyValue) < 0.001 then  //assume MyValue is zero.  
    

    If you absolutely cannot switch to decimal, you can do something similar. If MyDouble is within Epsilon of Zero, where Epsilon is a const in the neighborhood of 0.00001, then force your value to zero. This is not recommended is 99% of scenarios. You really MUST use Decimal for currency, otherwise you will end up charging customers the wrong amount or suffering seemingly-random bugs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a switch statement that executes some logic over and over. Rather then
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
I have a switch statement similar to this one: switch (number) { case 1:
I have kept hearing this statement. Switch..Case is Evil for code maintenance, but it
I have a switch statement that I am using in the drop down menus
What is the best way to reuse switch logic. I have this switch statement
I have a switch statement that takes the letter grade and returns the corresponding
Let's say I have a switch statement like this: switch($myVar) { case 'A': $special
I am writing some code in VB.NET that uses a switch statement but in
I have this If statement and I would like to move them into Case-Switch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.