4

I have a text box and I have bonded the text property to double value in my view model class. I have also create the regular expression for text box so that only numeric values would be entered.

I am calculating the value in my view model class. If value is less then or equal to 0.00001 then it shows the exponential format like (1.E-5) in text box.

I want to display the double value like 0.00001 in text box, not in exponential format. I also don't want to use string format i.e, D, F2, F5. because it restrict the number of floating point. How can I resolve it?

  • But decimal values are so hard to read when there are too many zeros. Scientific notation makes it so much easier when you directly see the magnitude, without having to count zeros manually. – Matti Virkkunen Dec 31 '12 at 12:58

2 Answers2

1

You should play with the StringFormat property of a Binding . Your TextBox XAML should look like this, if you want it to show 2 numbers after the dot:

<TextBox Text="{Binding YourText, StringFormat=N2}" />

If you need more information about StringFormat, I encourage you to read the MSDN article on Standard Numeric Format Strings

Damascus
  • 6,553
  • 5
  • 39
  • 53
  • Thanks for reply. I tried StringFormat but N2 will restrict the number of digit after dot. I don't want to restrict the floating numbers. – Ashish Singhal Jan 02 '13 at 05:47
  • There is no specific way to let an unlimited number of digits in a number, but the closest solution is to give as a StringFormat a custom one specifying a lot of digits. You can check the answer here for example: http://stackoverflow.com/questions/7795161/c-sharp-how-to-string-format-decimal-with-unlimited-decimal-places – Damascus Jan 02 '13 at 14:49
0

I know that this is not a solution, but it might help. You may use a method that gets a string like this and changes its format

string expon2notexpon(string num)
{
    num=num.ToLower();
    int index=num.IndexOf('e');
    if (index == -1)
        return num;

    string firstNum=num.Substring(0, index-1);
    int count=int32.Parse(num.Substring(index+1));
    res="0.";
    for (int i=1;i<count;i++)
    {
        res += '0'
    }
    res += firstNum.ToString();
}

I just typed the idea and there might be some bugs.

Ramin
  • 2,133
  • 14
  • 22