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 7506291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:13:02+00:00 2026-05-29T22:13:02+00:00

My boss reported my a bug today because my configuration was decreasing whenever he

  • 0

My boss reported my a bug today because my configuration was decreasing whenever he wanted to specify a value below 5%. I know that I can just round my number before casting it as int to fix my problem, but I don’t understand why this problem occurs.

I have a app.config file with the value “0.04” and a configuration section with a float property. When the section is read, the float value retrieved is 0.04, which is fine. I want to put this value in a windows forms TrackBar which accept an integer value so I multiply my value by 100 and a cast it as int. For some reason, the result is not 4, but it’s 3. You can test it like this :

Console.WriteLine((int)(float.Parse("0.04", System.Globalization.CultureInfo.InvariantCulture) * 100)); // 3

What happened?

  • 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-05-29T22:13:03+00:00Added an answer on May 29, 2026 at 10:13 pm

    It’s because 0.04 can’t be exactly represented as a float – and neither can the result of multiplying it by 100. The result is very slightly less than 4, so the cast to int truncates it.

    Basically, if you want to use numbers represented accurately in decimal, you should use the decimal type instead of float or double. See my articles on decimal floating point and binary floating point for more information.

    EDIT: There’s something more interesting going on here, actually… in particular, if you assign the result to a local variable first, that changes the result:

    using System;
    using System.Globalization;
    
    class Test
    {
        static void Main()
        {
            // Assign first, then multiply and assign back, then print
            float f = Foo();
            f *= 100;
            Console.WriteLine((int) f); // Prints 4
    
            // Assign once, then multiply within the expression...
            f = Foo();
            Console.WriteLine((int) (f * 100)); // Prints 4
    
            Console.WriteLine((int) (Foo() * 100)); // Prints 3
        }
    
        // No need to do parsing here. We just need to get the results from a method
        static float Foo()
        {
            return 0.04f;
        }
    }
    

    I’m not sure exactly what’s going on here, but the exact value of 0.04f is:

    0.039999999105930328369140625
    

    … so it does make sense for it not to print 4, potentially.

    I can force the result of 3 if the multiplication by 100 is performed with double arithmetic instead of float:

    f = Foo();
    Console.WriteLine((int) ((double)f * 100)); // Prints 3
    

    … but it’s not clear to me why that’s happening in the original version, given that float.Parse returns float, not double. At a guess, the result remains in registers and the subsequent multiplication is performed using double arithmetic (which is valid according to the spec) but it’s certainly a surprising difference.

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

Sidebar

Related Questions

My boss does not ever want to hear the word refactoring again, because whenever
My boss is asking me to code a report that has the following components:
My boss believes that wizards make things simple for the user. I think they
My boss is interested in using MKS Integrity for bug tracking, feature requests, Wiki
My boss is asking me to develop a demo application that uses log4net for
My boss gave me a bunch of requirements that I'm not pretty sure about
My boss has a windows application that he wrote. It is not a Windows
My boss has given me an assignment that I'm not sure is possible since
At work, I have started working on a program that can potentially generate hundreds
my boss wants me to code an URLrewriting for, let's say http://our.domain.com/SomeText -- that

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.