Question 0.3 / 3.0 why not equal to 0.1

Tatb

New member
Joined
May 11, 2021
Messages
1
Programming Experience
Beginner
This code for test on both vs2017 (Console .Net Framework) and vs2019 (Console .Net Core) .
code:
 static void Main(string[] args)
        {
            double a = 0.3d;
            double b = 3d;
            double c = a / basdf;

            Console.WriteLine($"a={a}, b={b}");
            Console.WriteLine(c);
            Console.ReadKey();
        }
Output :
vs2017output:
a=0.3, b=3
c=0.1
vs2019output:
a=0.3, b=3
c=0.09999999999999999
Why it is?
Should I keep using .Net Core? or I just avoid double type. or, Can I config IDE to fix them?
doubleType.png
 
Should I keep using .Net Core? or I just avoid double type. or, Can I config IDE to fix them?
You can keep using .Net Core, it is the type you are using.
float and double will give you the best precition for a floating point number using the space that they are using in memory. You can use them e.g. in games for values where thier decimal representation doesn't matter that much.
If it does matter, e.g. in accounting applications, you better use decimal. Every number that can be assigned to decimal does have a decimal representation.
Check this out to see the effect in live.
 
Back
Top Bottom