-2

I am trying to round off below decimal number upto 4 decimal point. the below decimal no is

1.0715086071862673E+301 but when i am using Math.Round function.it's not working and returning same above no.please let me know how to round this no.

**code here:**
double s=2.0;
double ku = Math.Pow(s, 1000);
double jlk = Math.Round(ku, 4);

here depending on my logic i need only 1.0715 number.

user3310138
  • 137
  • 5
  • 20
  • 3
    Where is the code? How did you specify the number? As a decimal, float,double or var? How did you call Math.Round? Are you sure you are checking the result of Math.Round instead of the original value? – Panagiotis Kanavos Mar 04 '14 at 08:58
  • that is quite a large number. is that a long? – Brett Caswell Mar 04 '14 at 09:06
  • You can't change what's stored in the `double` variables - but you can control the number of digits shown when you convert it to a `string`. – Damien_The_Unbeliever Mar 04 '14 at 09:10
  • this has got to be a string. a ulong would be 2^64, or 18446744073709551616 at max. – Brett Caswell Mar 04 '14 at 09:10
  • 3
    2 to the power 1000 is 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376. What are you hoping to get as a result from the `Math.Round` call? – AakashM Mar 04 '14 at 09:10
  • First, this is not rounding. Rounding is done after the decimal places. Since this is an integer, rounding it gives the same result as input. E.g. Round 10, result is 10. Round of 1000 is 1000 and so on. – Sunny R Gupta Mar 04 '14 at 09:13
  • nevermind.. this value can be a double +-1.7 E 308 – Brett Caswell Mar 04 '14 at 09:13
  • The Dot within `1.07...` does not mean that you will round your number to 1! The important part is `E+301`, which shows you that your number `1.07..` will get multiplied with 100000... (301 zeros following after the 1). What did you expect? – ElGauchooo Mar 04 '14 at 09:15

3 Answers3

2

Why do you round it ? Its a whole number anyway.

using System;
// referencing System.Numerics.dll
using System.Numerics;

namespace ConsoleApplication1
{
    class Program 
    {
        static void Main(string[] args) 
        {
            BigInteger bigInt = new BigInteger(2);
            bigInt = BigInteger.Pow(bigInt, 1000);
            Console.Out.WriteLine(bigInt.ToString());
        }
    }
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
0
Double val = 1.0715086071862673E+301
Math.Round(val / (10 ^ 297), 0) * (10 ^ 297)) 

The answer is going to be 1.0715E+301 ...


if you want it to be 1.0715, you would do

Double val = 1.0715086071862673E+301
Math.Round(val / (10 ^ 297), 0)

Additonally

This will work, but I wanted to point out that the conversion rounds in the process. (Also, this is vb.net - I'm not positive whether CType is how you convert in C#)

CType(CType((val / (10 ^ 297)) + 0.5, Long), Double) //Yields 1.0716
CType(CType((val / (10 ^ 297)), Long), Double)       //Yields 1.0715
Brett Caswell
  • 1,486
  • 1
  • 13
  • 25
  • And to get the 297 dynamically, one would use `Math.Floor(Math.Log10(val))-4`. Is `(10 ^ 297)` really what you think it is? – Lutz Lehmann Mar 04 '14 at 13:38
  • well.. it's 10 followed by 297 0's is about how I gauge it.. Thanks for the dynamic portion.. I didn't tackle it. I figured the OP was being a bit arbitrary on the whole precision aspect anyhow. rounding to the nearest 10^297 is one thing.. dropping a value by 10^297 is another. – Brett Caswell Mar 04 '14 at 15:43
  • As mathematical notation it is ok, and also in most CAS, but C like programming languages have ^ as bitwise "and", and script languages prefer x**y for the power operator. – Lutz Lehmann Mar 04 '14 at 15:56
-1

Use an appropriate type i.e. BigDecimal, src here Arbitrary-Precision Decimals in C#

Community
  • 1
  • 1
Boklucius
  • 1,896
  • 17
  • 19
  • Won't help. [`decimal`](http://msdn.microsoft.com/en-us/library/364x0z75.aspx) range is -7.9 x 10^28 to 7.9 x 10^28. It won't be able to store a number up in the 10^301s – Damien_The_Unbeliever Mar 04 '14 at 09:06