coze
coze

Reputation: 95

How to float.Parse or TryParse to a single Console.WriteLine in c#

Hello i'm new to programming and c# language. In the code bellow i'm trying to get used to coding and test stuff and see how it works.

        //excercise with variable y
        int y=2;
        Console.WriteLine("Given y = 2");
        Console.WriteLine("y++ : {0}", y++);
        Console.WriteLine("The value of y is now {0}", y);

        Console.WriteLine("Trying again y++: {0}", y++);
        Console.WriteLine("The valuee of y is now: {0}", y);

        Console.WriteLine("++y : {0}", ++y);                // ++ y Directly prints out y+1
        Console.WriteLine("The value of y is now: {0}", y);

        Console.WriteLine("y++ : {0}", y++);
        Console.WriteLine("The value of y is now : {0}", y);
        x += y;
        Console.WriteLine("lets add x and y: {0}+{1}", x, y);
        Console.WriteLine("A line with no calculations\n");

        y *= 99;
        Console.WriteLine("y *= 99 is: {0}", y);
        y /= 50;
        Console.WriteLine("y /= 50 is : {0}", y);
        y %= y;
        Console.WriteLine("The remainder of y is: {0}", y);

        Console.WriteLine("Lets see the value of y now: {0}", y);
        Console.WriteLine("++y is now : {0}", ++y);

        Console.ReadLine();
        Console.Clear();

At the following line i want it to be float like 11.88. But only for this line. I would usually do it with the variable but i'm wondering how to do it this way too.

y /= 50;
Console.WriteLine("y /= 50 is : {0}", y);

So could it be something like Console.WriteLine(float.Parse("y /=50 is: {0}",y);

Thank you guys for helping me!

Upvotes: 0

Views: 239

Answers (4)

Alex Sikilinda
Alex Sikilinda

Reputation: 3013

You can use either

Console.WriteLine((float)y/50);

or

Console.WriteLine(y/50f);

Because 50f is actually a float literal, y will be automatically converted to float. The second option looks better for me.

Added: actually the second option is preferred because it has only one type converstion: y from int to float. First option has two: the first on is explicit cast to float - (float)y, and implicit cast of 50 to float, because float can be diveded only by float.

Upvotes: 1

Sayse
Sayse

Reputation: 43320

Whilst casting to a float will fix your issue, your question was how to do float.Parse on a single line. The code you provided doesn't have any strings to parse but if it did then float.Parse does return a float so you can just use that directly in your calculations

float.Parse("1")/2.0f;

However, you should think if it is really necessary always to include everything on one line. The code you have provided is probably fine to be on one line but it can help the maintenance and upkeep of code by striving to keep it as readable as possible. Anywhere a line of code begins to just become lengthy, you may wish to start splitting it up into multiple lines or with temporary variables.

Upvotes: 0

H W
H W

Reputation: 2586

Do you mean this?

Console.WriteLine("y /= 50 is : {0}", (float)y/50);

I think what you were missing is that

y /= 50;

is nothing else than a short version of

y = y/50;

Edit: also like pointed out in the comments y/50 is an integer division. To convert the result to the float datatype, you need to either convert the divisor, or the whole result to float by using.

Upvotes: 0

user700390
user700390

Reputation: 2339

What you need to do is cast y to a float, then the division operation should similarly yield a float:

((float)y)/50

Upvotes: 2

Related Questions