Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2009
    Location
    Norway, Europe
    Posts
    94
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Simple C# Question

    I've coded my first program in C#. It's for finding the square root of a number. This is my code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("What number?");
                string dPre = Console.ReadLine();
                decimal dDec = Convert.ToDecimal(dPre);
                double d = Convert.ToDouble(dDec);
                double sqrtLol = Math.Sqrt(d);
                Console.WriteLine(sqrtLol);
                Console.ReadLine();
            }
            static void 
            
        }
    }
    However it seems quite trivial to convert from string to decimal, and decimal to double. Isn't there an easier way?
    Always Use a DOCTYPE!
    Always Use * {margin: 0; border: none; padding: 0;} in the top of your CSS stylesheet.
    Always Use only CSS for layout; never (X)HTML.
    Always Have fun while coding!

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Hi DarkLight

    You can convert from string to double this by using the parse method

    PHP Code:
    string dPre Console.ReadLine();
    double d double.ParsedPre ); 
    Please note that a FormatException might be thrown if the user enters non-numerical characters....so, I would use a try catch block to handle the user input

    Cheers
    ~E

  • #3
    New to the CF scene
    Join Date
    Oct 2010
    Location
    Slovenia
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •