From: gu.martinm@gmail.com Date: Sun, 27 Apr 2014 18:56:18 +0000 (+0200) Subject: decimal: never use float/double if decimals are important for you X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=ba47b833ccadcb0b3869e47bbb632ea8dfd79c48;p=CSharpForFun%2F.git decimal: never use float/double if decimals are important for you --- diff --git a/Allgemeines/Decimal/Decimal/Decimal.csproj b/Allgemeines/Decimal/Decimal/Decimal.csproj new file mode 100644 index 0000000..1ab5c83 --- /dev/null +++ b/Allgemeines/Decimal/Decimal/Decimal.csproj @@ -0,0 +1,42 @@ + + + + Debug + x86 + 12.0.0 + 2.0 + {6B44123C-847C-4DA5-9A04-71083AC7BA28} + Exe + Decimal + Decimal + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + x86 + false + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + \ No newline at end of file diff --git a/Allgemeines/Decimal/Decimal/Decimal.sln b/Allgemeines/Decimal/Decimal/Decimal.sln new file mode 100644 index 0000000..3a8c2a9 --- /dev/null +++ b/Allgemeines/Decimal/Decimal/Decimal.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Decimal", "Decimal.csproj", "{6B44123C-847C-4DA5-9A04-71083AC7BA28}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6B44123C-847C-4DA5-9A04-71083AC7BA28}.Debug|x86.ActiveCfg = Debug|x86 + {6B44123C-847C-4DA5-9A04-71083AC7BA28}.Debug|x86.Build.0 = Debug|x86 + {6B44123C-847C-4DA5-9A04-71083AC7BA28}.Release|x86.ActiveCfg = Release|x86 + {6B44123C-847C-4DA5-9A04-71083AC7BA28}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = Decimal.csproj + EndGlobalSection +EndGlobal diff --git a/Allgemeines/Decimal/Decimal/Program.cs b/Allgemeines/Decimal/Decimal/Program.cs new file mode 100644 index 0000000..930b8a1 --- /dev/null +++ b/Allgemeines/Decimal/Decimal/Program.cs @@ -0,0 +1,62 @@ +using System; +using System.Globalization; + +namespace Decimal +{ + class MainClass + { + // Like with BigDecimal in Java if DECIMALS ARE IMPORTANT FOR YOU NEVER USE DOUBLE OR FLOAT + // YOU MUST USE DECIMAL IN THE WHOLE APPLICATION. + + // C# IS NICER THAN JAVA IN ALMOST EVERY ASPECT!!!! COMPARE THIS TO MY BigDecimal EXAMPLES :( + + public static void Main(string[] args) + { + // With decimal, C# tries to use the decimals as you see them, without "loosing" values because of the IEE 754 + // It would be like the new BigDecimal(String) for Java being String = "165.01499999999998" + decimal fromDecimal = 165.01499999999998m; + string fromDecimalToString = fromDecimal.ToString("G", CultureInfo.InvariantCulture); + Console.WriteLine("fromDecimal: {0}", fromDecimalToString); + // 165.01499999999998 + // SCALE: 4 165.0149 + // AWAYFROMZERO (HALF UP BigDecimal Java): 0.9999999998 more than 0.5 then we should see as result 165.0150 + decimal fromDecimalRoundFour = Math.Round(fromDecimal, 4, MidpointRounding.AwayFromZero); + Console.WriteLine("fromDecimalRound four: {0}", fromDecimalRoundFour); + // 165.01499999999998 + // SCALE: 2 165.01 + // AWAYFROMZERO (HALF UP BigDecimal Java): 0.499999999998 less than 0.5 then we should see as result 165.01 + decimal fromDecimalRoundTwo = Math.Round(fromDecimal, 2, MidpointRounding.AwayFromZero); + Console.WriteLine("fromDecimalRound two: {0}", fromDecimalRoundTwo); + + // In the edges of your application you could have for example prices as strings + string stringDecimal = "165.01499999999998"; + // We could convert prices to decimal and work always with decimal for currency :) + decimal fromString = decimal.Parse(stringDecimal, NumberStyles.Float, CultureInfo.InvariantCulture); + string fromStringToString = fromString.ToString("G", CultureInfo.InvariantCulture); + Console.WriteLine("fromString: {0}", fromStringToString); + + + decimal fromShortDecimal = 165.015m; + string fromShortDecimalToString = fromShortDecimal.ToString("G", CultureInfo.InvariantCulture); + Console.WriteLine("fromShortDecimal: {0}", fromShortDecimalToString); + + // With doubles the things begin to be weird. As usual because the in memory value is not what you expect. + // For example: monodis Decimal/Decimal/bin/Debug/Decimal.exe + // In CIL we see: 165.01499999999979 We are already loosing some decimals. + // THIS IS WORSE THAN javac. WITH JAVA WHEN I WRITE double=165.0149999999998 I GET IN BYTECODE 165.015. + // The in memory value (IEE 754) for 165.0149999999998 and 165.015 is the same: 165.0149999999999863575794734060764312744140625 + // BUT WITH MONO I GET IN CIL: 165.0149999999997 AND ITS IN MEMORY VALUE IS DIFFERENT TO 165.0149999999998 + // OMG THIS COMPLETELY SUCKS!!!!! :/ + double fromDouble = 165.0149999999998; // IN CIL I HAVE 165.01499999999979, THE IN MEMORY VALUE IS DIFFERENT!!! SO, WE + // COULD FINISH HAVING A DIFFERENT RESULT!!!! WITH JAVAC I GOT 165.015 IN BYTECODE :( + // IT IS WORS THAN javac :( + Console.WriteLine("fromDouble: {0}", fromDouble); + + + // AS ALWAYS: ARE DECIMALS IMPORTANT FOR YOU? IF YES, YOU HAVE TO USE decimal + // WHEN DO YOU KNOW IF DECIMALS ARE IMPORTANT FOR YOU? IT DEPENDS... + // see: my example of BigDecimal in Java project, there I "try" to explain how to know when you have + // have to use fixed-point numbers. + } + } +} diff --git a/Allgemeines/Decimal/Decimal/Properties/AssemblyInfo.cs b/Allgemeines/Decimal/Decimal/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..afa35d5 --- /dev/null +++ b/Allgemeines/Decimal/Decimal/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle("Decimal")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("gumartinm.name")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("gumartinm.name")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] +