locked
System.Numeric.BigInteger RRS feed

  • Question

  • Is it possible to display a decimal value with the BigInteger class. I thought by maybe using BigInteger.Parse(string, NumberStyle) would work but it doesnt. Any help?
    Tuesday, December 15, 2009 3:43 AM

Answers

All replies

  • Parse is for reading values. For displaying use ToString. Decimal is the default.
    using System;
    using System.Numerics;
    class HelloWorld
    {
        static void Main(string [] args)
        {
            BigInteger i = BigInteger.Parse(args[0]);
            BigInteger j = BigInteger.Parse(args[1]);
            Console.WriteLine("  {0}", i);
            Console.WriteLine("+ {0}", j);
            Console.WriteLine("= {0}", i + j);
        }
    }
    

    -Karel
    Tuesday, December 15, 2009 4:00 AM
    Moderator
  • Thank you for the response. But the following throws an exception.
    BigInteger
    i = BigInteger.Parse("2.2"); 
    BigInteger j = BigInteger.Parse("4.8"); 
    Console.WriteLine(" {0}", i); 
    Console.WriteLine("+ {0}", j); 
    Console.WriteLine("= {0}", i + j);
    Tuesday, December 15, 2009 4:08 AM
  • Thank you for the response. But the following throws an exception.

    The BigInteger class is for integers.  Karel's code is correct and has no errors with valid arguments.
    Please don't double post.  Particularly after receiving an answer.  System.Numeric.BigInteger http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/9bf05fa6-21b5-41c5-8901-8fa5caf4f0ea
    Tuesday, December 15, 2009 7:51 AM
  • That's correct - BigInteger is not for real numbers - see the docs http://msdn.microsoft.com/en-us/library/system.numerics.biginteger(VS.100).aspx

    -Karel
    Tuesday, December 15, 2009 8:19 AM
    Moderator
  • Ok. well thank you for clarifying that for me. But I understand that Karels answer may have been correct in scope with that fact, but was not correct in the scope of my question. The correct answer would been, you simply cant. So I have no idea why karels answer, was marked as the correct answer for all it did was show how to get an integer value, when I ask for double.
    Tuesday, December 15, 2009 5:59 PM
  • Is it possible to display a decimal value with the BigInteger class. I thought by maybe using BigInteger.Parse(string, NumberStyle) would work but it doesnt. Any help?

    For farther clarification from Karel's link:

    using System;

    using System.Numerics;

    namespace ConsoleApplication4

    {

      class Program

      {

     

        static void Main(string [] args)

        {

            Decimal dec = new decimal(2.2);

            BigInteger i = new BigInteger(dec);

            dec = new decimal(4.8);

            BigInteger j = new BigInteger(dec);

            Console.WriteLine("  {0}", i);

            Console.WriteLine("+ {0}", j);

            Console.WriteLine("= {0}", i + j);

            double dbl = 2.2d;

            i = new BigInteger(dbl);

            dbl = 4.8d;

            j = new BigInteger(dbl);

            Console.WriteLine("  {0}", i);

            Console.WriteLine("+ {0}", j);

            Console.WriteLine("= {0}", i + j);

            Console.ReadLine();

        }

      }

    }

    Tuesday, December 15, 2009 6:58 PM
  • Yes I understand, that it is possible to pass a double value to the BigInteger class. But it rounds it off, and does not show the decimal value. Which, from beginning, was what I was asking how to do. And again, the answer would be, "you cant". So in turn, her "answer" was not correct to the scope of my question. So when other people come around asking the same question, and find her "answer", they will find, it was not what they were looking for, because its the correct answer for the question.
    Wednesday, December 16, 2009 3:27 AM
  • She thought you wanted to display (or parse) the BigInteger in decimal representation as in base 10, as opposed to base 16 or base 2.
    "A cat always lands feet first. Toast always lands butter side down. A cat with toast strapped on its back will hover above the air in a state of quantum indecision." ~Unknown
    Saturday, January 30, 2010 8:17 PM