Formating SQL Real to two decimal place RRS feed

  • Question

  • I have a database field which store real (e.g. 23.25252525). When I view the data in the table in server explorer I can see 23.25. However, when i run LINQ query to obtain the data and show it on the TextBlock, I cannot format it to 23.25. What I can see is 23.2525252525.


    I have tried using the folling three codes fragments, but the results are the same. However, these code when run on a ConsoleApplication they work fine.


    Try Out 1

    double d = Convert.ToDouble(b.Balance);

    string s = d.ToString("F");

    BalanceCheckingTextBlock.Text = s;



    Try Out 2

    double d = Convert.ToDouble(b.Balance);

    string s = d.ToString("N2");

    BalanceCheckingTextBlock.Text = s;



    Try Out 3

    System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;

    nfi.NumberDecimalDigits = 2;

    double ba = b.Balance;

    string s = ba.ToString("N", nfi);

    BalanceCheckingTextBlock.Text = s;



    However, when I ran these code fragment (not using DB) in a ConsoleApplication I can format the number of type double to two decimal places
    Friday, September 12, 2008 5:03 AM


  • This really confuse me. Somehow when I rerun the program again the next day, the output is printed out correctly using the code in "Try Out 2"
    Friday, September 12, 2008 1:40 PM