as74
as74

Reputation: 710

FormatString to display all zeros

Price value (double) is sometimes 1.23000 and sometimes 1.230. My ListView shows it as 1.23. Unfortunatelly numer of decimal points changes and I can't use i.e.: F5 or

StringFormat='{}{0:#,00000}'

How to use StringFormat to always show all zeros?

<TextBlock Text="{Binding Path=Price, StringFormat={Binding Path=DecimalPoints}}"  />

Upvotes: 0

Views: 194

Answers (1)

AlSki
AlSki

Reputation: 6961

Actually WPF is showing the correct value of the double.

If you try running

if (1.230000000d == 1.23d) throw new ArgumentException("Values are equal");

you will find that the values are always equal. As soon as the compiler converts the string representation to a double the extra zeros are gone.

You could try implementing your own Price class as this will enable you to keep the representation with the value,

public class Price
{
    public double Value {get;set;}
    public int precision {get;set;}

   public override ToString()
   {
        return value.ToString("F"+precision);
   }
}

Upvotes: 1

Related Questions