decimal: never use float/double if decimals are important for you
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 27 Apr 2014 18:56:18 +0000 (20:56 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 27 Apr 2014 18:56:18 +0000 (20:56 +0200)
Allgemeines/Decimal/Decimal/Decimal.csproj [new file with mode: 0644]
Allgemeines/Decimal/Decimal/Decimal.sln [new file with mode: 0644]
Allgemeines/Decimal/Decimal/Program.cs [new file with mode: 0644]
Allgemeines/Decimal/Decimal/Properties/AssemblyInfo.cs [new file with mode: 0644]

diff --git a/Allgemeines/Decimal/Decimal/Decimal.csproj b/Allgemeines/Decimal/Decimal/Decimal.csproj
new file mode 100644 (file)
index 0000000..1ab5c83
--- /dev/null
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProductVersion>12.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{6B44123C-847C-4DA5-9A04-71083AC7BA28}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>Decimal</RootNamespace>
+    <AssemblyName>Decimal</AssemblyName>
+    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <PlatformTarget>x86</PlatformTarget>
+    <ConsolePause>false</ConsolePause>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <DebugType>full</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+    <PlatformTarget>x86</PlatformTarget>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/Allgemeines/Decimal/Decimal/Decimal.sln b/Allgemeines/Decimal/Decimal/Decimal.sln
new file mode 100644 (file)
index 0000000..3a8c2a9
--- /dev/null
@@ -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 (file)
index 0000000..930b8a1
--- /dev/null
@@ -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 (file)
index 0000000..afa35d5
--- /dev/null
@@ -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("")]
+