C# Result Length RRS feed

  • Question

  • Hello, I a question about making equations between two numbers. If I divide 10 with 3 (10 / 3). I have this reuslt "3.33333333333333" with 15 numbers result.

    double i = (double)10000 / (double)3;

    I want the result number after an equation, has length 20 numbers. How I can change this?

    George Andredakis

    The limits of what a computer can do are difined only from programmer's imagination

    Wednesday, February 7, 2018 9:19 AM


  • Decimals have larger precision but less range.

    decimal j = (decimal) 10/3;
    Console.WriteLine("{0:0.0000000000000000000}", j);

    The result would normally be 28-29 significant digits but I've reduced that in the output by using a string format in Console.WriteLine.

    Note that all numeric datatypes have some kind of limit, either in range or the number of significant digits they can hold. You just have to choose the appropriate type, e.g. double, decimal, float etc for your needs (if nothing fits your requirements then you may have to use some more advanced maths library)

    • Edited by RJP1973 Wednesday, February 7, 2018 9:33 AM
    • Proposed as answer by Wizend Wednesday, February 7, 2018 9:46 AM
    • Marked as answer by George Andredakis Wednesday, February 7, 2018 9:50 AM
    Wednesday, February 7, 2018 9:32 AM