settingsAccountsettings
Menusettings

Q: Assigning floating point values to variables in C#

+5 votes

I must create a new project (in C#) and create a program that assigns floating point values to variables. I must find the most suitable variable type in order to save memory.

The numbers to print are:

3.141592653589793238

1.60217657

7.8184261974584555216535342341

asked in C# category by user Jolie Ann

2 Answers

+2 votes
 
Best answer
using System;

class FloatingPointNumbers
{
    static void Main()
    {
        decimal a = 3.141592653589793238M;
        double b = 1.60217657;
        decimal c = 7.8184261974584555216535342341M;

        Console.WriteLine("{0}\n{1}\n{2}", a, b, c);
    }
}

Be careful as float (32-bits) - shows 7 numbers;

double (64-bits) shows 15-16 numbers;

the biggest is decimal (128-bit) - 28 to 29 numbers

answered by user eiorgert
edited by user golearnweb
+1 vote

4 more numbers to assign :-):

using System;

class FloatOrDouble
{
    static void Main()
    {
        double a = 34.567839023;
        float b = 12.345f;
        double c = 8923.1234857;
        float d = 3456.091f;

        Console.WriteLine("{0}\n{1}\n{2}\n{3}\n", a, b, c, d);
    }
}

 

answered by user andrew
edited by user golearnweb
...