Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo everybody. I am facing a problem to display a double type value to its exact format.

The number is 1/983. I want to print it upto 2000 decimal places. I am using C#. Using shell scripting I got the result:

scale=2000
1/983
0.(00101729399796541200406917599186164801627670396744...)

Is there any way in C# to get the result. I try (1/983).ToString(), but I get 0.00101729399796541. I found a class in the link http://www.yoda.arachsys.com/csharp/floatingpoint.html[^] but it can display only
0.0010172939979654119745100526728265322162769734859466552734375. Please help.

JSOP Edit - I cut off the unnecessary extra digits (we got your point)
Posted
Updated 14-Jan-10 2:07am
v3

I would have used a StringBuilder instead of a LinkedList, and decimal instead of int...

decimal Rem = 10;
decimal Number = 983;
int MAXLEN = 2000;
StringBuilder result = new StringBuilder("0.");
for (int i = 0; i < MAXLEN; i++)
{
	result.Append((Rem / Number).ToString()[0]);
	Rem = (Rem % Number) * 10;
}
 
Share this answer
 
Why won't this work?

int    divisor  = 983;
int    dividend = 1;
int    scale    = 2000;
string padding  = "";

padding = string.Format("{0}{1}{2}", "{0:0.", padding.PadRight(scale, '0'), "}");

string result = string.Format(padding, (double)dividend / (double)divisor);


Of course, you're probably aware that doing math on floating point numbers is highly inconsistent, and you should use a decimal type instead (even though you won't be able to get 2000 insignificant digits out of a decimal).

EDIT ============

Using decimal types for the division results in more precision, but it seems my solution won't derive the desired result. I even tried making the dividend and divisor Int64 types, and it still didn't matter.

Using doubles gives me this:

0.00101729399796541 (followed by the appropriate number of zeros)

And using decimals gives me this:

0.001017293997965412004069176 (followed by the appropriate number of zeros)
 
Share this answer
 
v8
A double cannot store your number with the requested precision (see http://en.wikipedia.org/wiki/IEEE_754-1985) that's why the class you found fails at.
Anyway, using basic math integer operations, you may compute yourself how many decimal digits you like.
:)
 
Share this answer
 
It is solved.

  LinkedList<string> List = new LinkedList<string>();<br />
        int i;<br />
        int Rem=10;<br />
        int Number=983;<br />
        int MAXLEN=2000;<br />
<br />
        List.AddLast("0.");<br />
        for (i = 1; i <= MAXLEN; i++)<br />
        {<br />
            List.AddLast((Rem / Number).ToString());<br />
            Rem = (Rem % Number) * 10;<br />
        }<br />
<br />
        foreach (string str in List)<br />
        {<br />
            Console.Write(str);<br />
        }<br />


Thank you.
 
Share this answer
 
@ John Simmon, I get this :

0.00101729399796541000...

EDIT rom JSOP - Yeah, I know. You don't have to include all the zeros - it makes the web site scroll horizontally, and that just ticks people off.
 
Share this answer
 
v4
@ John Simmon.

Thank you very much sir. I learn this StringBuilder class.
 
Share this answer
 
Here's a better answer:

This is your version. It takes 2000 calculations and string moves, and generates incorrect results to boot:

static string Method1()
{
    int iterations = 0;
    decimal Rem = 10;
    decimal Number = 983;
    int MAXLEN = 2000;
    StringBuilder result = new StringBuilder("0.");
    for (int i = 0; i &lt; MAXLEN; i++)
    {
        result.Append((Rem / Number).ToString()[0]);
        Rem = (Rem % Number) * 10;
        iterations++;
    }
    // this takes 200 iterations (and generates incorrect results)
    return result.ToString();
}


This is my version, which only takes 135 iterations, and generates correct results:

static string Method2()
{
    int iterations = 0;
    double dividend = 10;
    double divisor  = 983;
    double result   = 0;
    int    MAXLEN   = 2000;
    StringBuilder text = new StringBuilder("0.");
    do
    {
        result = dividend / divisor;
        string temp = string.Format("{0:0.0000000000000000}", result);
        if (result &gt;= 1d)
        {
            temp = (result &gt;= 1d) ? temp.Substring(0, temp.IndexOf('.')) 
         		     : temp.Substring(temp.IndexOf('.')+1);
        }
        text.Append(temp);
        dividend = (dividend % divisor) * Math.Pow(10d, 15d);
        iterations++;
    } while (text.Length &lt; MAXLEN + 2);
    // this takes 135 iterations (and generates correct results)
    return text.ToString().Substring(0, MAXLEN);
}
 
Share this answer
 
v2
I am getting error here:

if (result &gt;= 1d)
temp = (result &gt;= 1d) ? temp.Substring(0, temp.IndexOf('.'))
while (text.Length &lt; MAXLEN + 2);

Can you please tell me what is &amp;gt;= 1d ?
 
Share this answer
 
Look at the text your tried to copy, and you'll see the appropriate characters.

When you copy the code from the screen, it's substituting the escape codes for the < and > characters.
 
Share this answer
 
Run it through the debugger and find out.
 
Share this answer
 
Sir I solve it by removing extra “.” .Here is the code:
<br />
using System;<br />
using System.Text;<br />
<br />
class p<br />
{ <br />
    static string Method2()<br />
{<br />
    int iterations = 0;<br />
    double dividend = 10;<br />
    double divisor  = 983;<br />
    double result   = 0;<br />
    int    MAXLEN   = 2000;<br />
    StringBuilder text = new StringBuilder("0.");<br />
<br />
    do<br />
    {<br />
        result = dividend / divisor;<br />
        string temp = string.Format("{0:0.0000000000000000}", result);<br />
                <br />
        <br />
        if (result >= 1d)<br />
        {<br />
            temp = (result >= 1d) ? temp.Substring(0, temp.IndexOf('.')) : temp.Substring(temp.IndexOf('.') + 1);<br />
        }<br />
        <br />
        text.Append(temp);<br />
        <br />
        dividend = (dividend % divisor) * Math.Pow(10d, 15d);<br />
        iterations++;<br />
    } while (text.Length < MAXLEN + 2);<br />
<br />
    // this takes 135 iterations (and generates correct results)<br />
    text.Remove(3,1); 	//I add this line<br />
    return text.ToString().Substring(0, MAXLEN);<br />
<br />
}<br />
    static void Main()<br />
    {<br />
        Console.WriteLine(Method2());<br />
    }<br />
}<br />


But it seems that the result is incorrect. I am tring to give a little comparison:
bc command : 0.00101729399796541200406917
My code using linked list : 0.00101729399796541200406917
Your second last code : 0.00101729399796541200406917
Your last code : 0.0.0101729399796541101729399
Your last code after my edit : 0.00101729399796541101729399

The error occurred after 17 decimal places. What is your opinion Sir?

One more request Sir please explain this portion:
<br />
if (result >= 1d)<br />
{<br />
     temp = (result >= 1d) ? temp.Substring(0, temp.IndexOf('.')) : temp.Substring(temp.IndexOf('.') + 1);<br />
}<br />


What is the need of using "(result >= 1d)" two times?
 
Share this answer
 
The output of my code is
0.(001017293997965412004069175991861648016276703967446592065106815869786
368260427263479145473041709053916581892166836215666327568667344862665310
274669379450661241098677517802644964394710071210579857578840284842319430
315361139369277721261444557477110885045778229908443540183112919633774160
732451678535096642929806714140386571719226856561546286876907426246185147
507629704984740590030518819938962360122075279755849440488301119023397761
953204476093591047812817904374364191251271617497456765005086469989827060
020345879959308240081383519837232960325534079348931841302136317395727365
208545269582909460834181078331637843336724313326551373346897253306205493
387589013224821973550356052899287894201424211597151576805696846388606307
222787385554425228891149542217700915564598168870803662258392675483214649
033570701932858596134282807731434384537131230925737538148524923702950152
594099694811800610376398779247202441505595116988809766022380467955239064
0895218718209562563580874872838250254323499491353)

I am trying to solve Project Euler Problem 26 http://projecteuler.net/index.php?section=problems&id=26[^]. This output is partially related to that problem. I get the proper output without this. It s just out of curisity. Can you please tell me that use of my code is inappropriate / inefficient?
 
Share this answer
 
v3
Sir I am getting this output:
0.0.01017293997965411017293997965412004069175991961648016276704067446592
065106815869786368260427263479145473041709053916581992166836215666327568
667344862765310274669379550661241098677517802644964394710071210579857678
840284842319430315361139369377721261444557577110885045778229908443540183
112919633774160732451678535096642929806714140486571719226856661546286876
907426246185147507629704984740590030518819938962460122075279755949440488
301119023397761953204576093591047812817904374364191351271617497456865005
086469989827060020345880059308240081383519837232960325534079348931841321
363173957273652085452695829946083418107833163784333672431332655137334699
725330620549348758901322482207355035605289938789420142421169715157680569
694638860630722288738555442522899114954221770091556459816887083662258392
675483214649033570719328585961342828077314343845371312309257375381485249
237030501525940996948118006103763988792472024415056951169888097660223804
679552391640895218718210562563580874873838250254323500491353001017294997
965412004069175991861648016276703967446592651068158697863682604272634791
454730417090549165818921668362156663275686673448626653102756693794506612
419867751780264596439471007121157985757884028584231943031536113936927772
126144455747711088545778229908443540183112919634774160732451679535096642
929807714140386571719226856561546287876907426246185147507629704985740590
030518820938962360122075279755849440488301119023397762953204476093591478
128179043743641912512716184974567650050874699898270600203458799593082408
138351983723396032553407934993184130213631739572736520854526958290946083
418107833163784333672431332655137334689725330620549338758901322482197355
035652899287894201424211597151577805696846388606307222787385554425228891
149542217700915564598168870803662258392675483214649335707019328585961342
828077314343845371312319257375381485259237029501525949969481180061037639
877924720244150559511698980976602238046895523906408952287182095625635887
48728382502543234994913530010172939979654120040691759918

What to do for 0.0.0 at the beginning?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900