{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Numbers and Decimals\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=kdKcpF9roeU&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=7) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Working With Fixed Point types\n", "\n", "The `Decimal` type is similar to `double`s. They don't have as big a range, but they do have much higher precision.\n", "\n", "> What's the range of a decimal type?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335\r\n" ] } ], "source": [ "decimal min = decimal.MinValue;\n", "decimal max = decimal.MaxValue;\n", "Console.WriteLine($\"The range of the decimal type is {min} to {max}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> How do doubles and decimals compare in precision?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3333333333333333\r\n", "0.3333333333333333333333333333\r\n" ] } ], "source": [ "double a = 1.0;\n", "double b = 3.0;\n", "Console.WriteLine(a / b);\n", "\n", "decimal c = 1.0M;\n", "decimal d = 3.0M;\n", "Console.WriteLine(c / d);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see that a decimals have a higher precision than doubles." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "\n", "> Can you find the area of a circle with a radius of 2.5 inches?\n", ">\n", "> What does the answer look like when you change number types, such as int, short, and decimal.\n", ">\n", "> Hint: You can write `Math.PI` as pi, instead of writing the answer out." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Branches (if)](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/07-Branches%20(if).ipynb)\n", ">\n", "> [⏪ Last Module - Numbers and Integer Precision](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/05-Numbers%20and%20Integer%20Precision.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=kdKcpF9roeU&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=7)\n", ">\n", "> [Documentation: Numbers in C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/numbers-in-csharp?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }