How to Read a Float Value in C#

Coding superpowers

Behavior as Usual

My mood was much better subsequently visiting the basement. In this nighttime and grim world, I finally had at least i hope to concord onto - the hope that at that place was at least a chance that I could make it back to my ain time.
For a few days after getting into the basement and dorsum out once more, I engaged in some people watching whenever I was moving around the base. By the looks of it, no one had particularly noticed my absence, which meant that I should be able to proceed helping Noname to restore his modules without drawing suspicion.

Jessica was heading toward me in the corridor. "Howdy, Teo. You look then enthusiastic today!"
"Thanks, you look pretty chipper yourself!" I returned. I suddenly noticed that everyone other than me was headed in the other direction. "Where's everyone going?" I asked.
"Ritchie returned from an expedition and said he had something to show off. He chosen a meeting in the large kitchen at..." Jessica looked at her picket, "At present! Allow'southward go!" I turned on my heels and tried to continue upwards.

The Types

Ritchie was continuing at the front of the kitchen, and most of the tables were full. People were still trickling in piffling by fiddling, and everyone was waiting for him to begin. Finally:
"Hello, anybody! Give thanks you all for coming on such short observe. I've just returned from the machine city, and an ally of ours shared some intel they've been working on obtaining for a while. It turns out there are many more types than just bool , int , and cord .

"More than? Actually?" Someone from the crowd called. Ritchie replied, "Yes, many more. There are and so-called built-in types , which are more complex types than string and the other types we already know. Permit's start simple and go over some of the built-in types, the get-go of which are the float and double types. Both of these types represent a real number, sometimes referred to as a floating-point number (this is where the proper noun 'float' goes from). For example," at this, Ritchie uncapped a marker and began writing on the whiteboard backside him, "numbers like ii.5, 17.9456, and 0.7 are real numbers."

"Why would we demand such complexity?" someone asked, mirroring my ain thoughts. "I use int in all my programs and everything seems to work fine!"

Ritchie had clearly anticipated this question. "In our world, the primary currency is viruses. Every number of viruses is an integer number. You tin take ii, 5, or 10 viruses, but not ii.5, right? That's why you call back in terms of the int type. Earlier our time though, people had a currency - the dollar - to calculate their money. You could accept 1.fifty dollars, or 1.63 dollars, not just 1 or 2. In a similar fashion, you can use a double variable to store a existent number inside:"

                                    double                        myMoney            =            187.543            ;                  

He continued, "I know it seems complicated, merely you lot can add, subtract, split, and multiply double variables same every bit you lot exercise with integers."

                                    double                        myMoney            =            10.25            ;                          double                        fourTimesMyMoney            =            10.25            *            4.0            ;            Console.            WriteLine            (fourTimesMyMoney)            ;            // Outputs 41                          double                        decreasedMoney            =            myMoney            -            1.25            ;            Console.            WriteLine            (decreasedMoney)            ;            // Outputs 9                          double                        dividedMoney            =            decreasedMymoney            /            two            ;            Console.            WriteLine            (dividedMoney)            ;            // Outputs four.v                          double                        addToMyMoney            =            dividedMoney            +            100.34            ;            Console.            WriteLine            (addToMyMoney)            ;            // Outputs 104.84                  

Ritchie continued making the case for the new types. "Even better," he said, "you tin can add, decrease, split up, and multiply double and int variables together. The effect is automatically converted to a double type."

                                    double                        someDouble            =            seven.five            ;                          int                        someInt            =            2            ;                          double                        sum            =            someDouble            +            someInt;            Panel.            WriteLine            (sum)            ;            // Outputs 9.v                          double                        decrease            =            someDouble            -            someInt;            Console.            WriteLine            (subtract)            ;            // Outputs five.five                          double                        multiply            =            someDouble            *            someInt;            Panel.            WriteLine            (multiply)            ;            // Outputs 15                          double                        separate            =            someDouble            /            someInt;            Panel.            WriteLine            (divide)            ;            // Outputs 3.75                  

This isn't any different than how you do things outside of coding. Nosotros can add integers and real numbers, subtract them, divide them, and multiply them without any hiccups. At the same fourth dimension, y'all cannot multiply strings with any other type, or divide or subtract. You tin can only add strings (using "+") to numeric types . Here are some examples:

                                    double                        someDouble            =            7.v            ;                          int                        someInt            =            2            ;                          string                        someString            =            "Hello "            ;                          double                        result1            =            someDouble            +            someInt;            // Ok - calculation double and int                          string                        result2            =            someString            +            someDouble;            // Ok - append double to cord                          string                        result3            =            someString            +            someInt;            // Ok - suspend int to string                          string                        result4            =            someString            *            someInt;            // NOT ok - can't multiply int and            // cord                          cord                        result5            =            someString            /            someDouble;            // NOT ok - can't separate cord on            // double                          cord                        result6            =            someString            *            someString;            // NOT ok - tin't multiply strings                  

To output a double to the screen or concatenate information technology with a cord, you utilise exactly the aforementioned mechanisms every bit you used with an int .

                                    double                        temperature            =            17.5            ;            Panel.            WriteLine            (temperature)            ;            // Outputs: 17.5            Console.            WriteLine            (            "Temp is "            +            temperature)            ;            // Outputs: Temp is 17.5            Panel.            WriteLine            (                          $"Temp is                                            {                temperature                }                            "                        )            ;            // Outputs: Temp is 17.v                  

Everyone in the room was nodding. Ritchie asked us to attempt creating a double variable and then output it to the screen.

If yous want to read a double from the screen, use the method double .Parse(...) the same equally you used int .Parse(...). Here is an example:

                                    string                        numberString            =            Console.            ReadLine            (            )            ;                          double                        number            =            double            .            Parse            (numberString)            ;                  

Round hatch

"What about the bladder type?" asked someone backside me.

A float is almost the same as a double . The main divergence is that a double is twice as large as a float in memory space (hence the name 'double'). This results in a dissimilar range of values that you lot can store in these two types:

Type Space in Memory Approximate Range Precision Default Value Example
float 4 bytes -3.4 x 10 38 to +3.4 10 10 38 seven 0.0 f 0.1234567 f
double 8 bytes ±5.0 x 10 -324 to ±1.7 ten 10 308 16 0.0 0.1234567890123456

Past default, every real number in C# program is treated every bit a double . To force C# to recognize a number as a float, you must add together an "f" at the cease of every float, as shown in the to a higher place table and the beneath example.

You tin can add, subtract, multiply, and carve up double and bladder variables the aforementioned as yous tin can with doubles and ints. The result will once more be a double blazon.

                                    double                        someDouble            =            1.5            ;                          float                        someFloat            =            2.5f            ;                          double                        sum            =            someDouble            +            someFloat;            Console.            WriteLine            (sum)            ;            // Outputs 4                          double                        decrease            =            someDouble            -            someFloat;            Panel.            WriteLine            (decrease)            ;            // Outputs -ane                          double                        multiply            =            someDouble            *            someFloat;            Console.            WriteLine            (multiply)            ;            // Outputs 3.75                          double                        carve up            =            someDouble            /            someFloat;            Panel.            WriteLine            (split up)            ;            // Outputs 0.half dozen                  

After walking around to make sure we had grasped the new concepts, Ritchie returned to the front of the room. "From now on," he began, "we're going to apply the double type because it'due south as fast as the float type, but provides much more precise results. I suggest that you all get comfortable using the double in your programs, and only utilize a float if you have a very skillful reason to do and so."

"Let'due south wrap up the first part of our meeting," Ritche said. "Consummate this next task and then have a half-hour lunch interruption. Nosotros'll get into more than new types this afternoon!"

Programmer table

bradenscoged1999.blogspot.com

Source: https://codeasy.net/lesson/double_type_introduction

0 Response to "How to Read a Float Value in C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel