{"id":120,"date":"2021-12-22T15:36:46","date_gmt":"2021-12-22T08:36:46","guid":{"rendered":"https:\/\/csharptutorial.net\/?page_id=120"},"modified":"2022-01-10T08:42:08","modified_gmt":"2022-01-10T01:42:08","slug":"csharp-float","status":"publish","type":"page","link":"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-float\/","title":{"rendered":"C# float"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the C# float types to represent floating-point numbers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the C# float types<\/h2>\n\n\n\n<p>To represent real numbers, C# uses the following floating number types: <code>float<\/code>, <code>double<\/code> and <code>decimal<\/code>. The following table shows the characteristics of the floating-point types:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Float type<\/th><th>Approximate range<\/th><th>Precision<\/th><th>Size<\/th><\/tr><\/thead><tbody><tr><td><code>float<\/code><\/td><td>\u00b11.5 x 10<sup>\u221245<\/sup>&nbsp;to \u00b13.4 x 10<sup>38<\/sup><\/td><td>~6-9 digits<\/td><td>4 bytes<\/td><\/tr><tr><td><code>double<\/code><\/td><td>\u00b15.0 \u00d7 10<sup>\u2212324<\/sup>&nbsp;to \u00b11.7 \u00d7 10<sup>308<\/sup><\/td><td>~15-17 digits<\/td><td>8 bytes<\/td><\/tr><tr><td><code>decimal<\/code><\/td><td>\u00b11.0 x 10<sup>-28<\/sup>&nbsp;to \u00b17.9228 x 10<sup>28<\/sup><\/td><td>28-29 digits<\/td><td>16 bytes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Equality test<\/h3>\n\n\n\n<p>Since computers only can store the floating-point numbers approximately, it&#8217;ll cause unexpected behavior if you attempt to compare two float numbers. <\/p>\n\n\n\n<p>For example, the following expression should return <code>true<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-number\">0.3<\/span> == <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">01<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>But it returns <code>false<\/code> instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">bool<\/span> result = <span class=\"hljs-number\">0.3<\/span> == <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">01<\/span>;\nConsole.WriteLine(result); <span class=\"hljs-comment\">\/\/ false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The reason is that the expression returns a value that is approximately equal to 0.3, not 0.3. See the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\">Console.WriteLine(<span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span> + <span class=\"hljs-number\">0.1<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-number\">0.30000000000000004<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Float literals<\/h2>\n\n\n\n<p>Each float type has a specific literal form. And all float literals can have the digit separator (<code>_<\/code>) to make them more readable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">float<\/h3>\n\n\n\n<p>The float literals have the <code>f<\/code> or <code>F<\/code> suffix. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">float<\/span> rate = <span class=\"hljs-number\">5.2F<\/span>;\n<span class=\"hljs-keyword\">float<\/span> amount = <span class=\"hljs-number\">10<\/span>_000<span class=\"hljs-number\">.5f<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">double<\/h3>\n\n\n\n<p>The double literals have no suffix. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">double<\/span> dimension = <span class=\"hljs-number\">3.14<\/span>\n<span class=\"hljs-keyword\">double<\/span> radius = <span class=\"hljs-number\">1<\/span>_000<span class=\"hljs-number\">.5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Or with the <code>d<\/code> or <code>D<\/code> suffix like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">double<\/span> dimension = <span class=\"hljs-number\">3.14<\/span>d\n<span class=\"hljs-keyword\">double<\/span> radius = <span class=\"hljs-number\">1<\/span>_000<span class=\"hljs-number\">.5<\/span>D<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">decimal<\/h3>\n\n\n\n<p>The decimal literals have <code>m<\/code> or <code>M<\/code> suffix:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C#\" data-shcb-language-slug=\"cs\"><span><code class=\"hljs language-cs\"><span class=\"hljs-keyword\">decimal<\/span> amount = <span class=\"hljs-number\">9.99<\/span>m\n<span class=\"hljs-keyword\">decimal<\/span> tax = <span class=\"hljs-number\">0.08<\/span>M<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C#<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cs<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Conversions<\/h2>\n\n\n\n<p>C# implicitly converts a value of float to double. However, you can use an explicit cast to convert a value from one floating-point type to another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>C# uses the <code>float<\/code>, <code>double<\/code>, and <code>demical<\/code> types to represent real numbers.<\/li><li>Avoid using the equality operator <code>==<\/code> to compare two real numbers. <\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"120\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-float\/\"\n\t\t\t\tdata-post-title=\"C# float\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"120\"\n\t\t\t\tdata-post-url=\"https:\/\/www.csharptutorial.net\/csharp-tutorial\/csharp-float\/\"\n\t\t\t\tdata-post-title=\"C# float\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the C# float types to represent floating-point numbers.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":7,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-120","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":5,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/120\/revisions"}],"predecessor-version":[{"id":488,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/120\/revisions\/488"}],"up":[{"embeddable":true,"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/pages\/7"}],"wp:attachment":[{"href":"https:\/\/www.csharptutorial.net\/wp-json\/wp\/v2\/media?parent=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}