Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

thousand separator formatting int, float, decimal values RSS

4 replies

Last post Mar 03, 2009 12:31 PM by hazz

(0)
  • hazz

    hazz

    Member

    245 Points

    436 Posts

    thousand separator formatting int, float, decimal values

    Mar 02, 2009 08:35 PM | LINK

    I have numeric values from the database that need thousand separator formatting.

    Do I need to create string datatypes for the properties to achieve this as per the following?

    so public Nullable<float> AvgLinePressure { get; set; } //real datatype in database.

    would become

    public string AvgLinePressure { get; set; }   //real datatype in database. ???????

    object.AvgLinePressure = String.Format(numericAvgLinePressure.ToString("#,##0,,", CultureInfo.InvariantCulture)) ;   //convert-format numeric db value

    All data values are being displayed in a DataGridTemplateColumn .

    Thank you, 

    hazz
  • msalsbery

    msalsbery

    Contributor

    4432 Points

    773 Posts

    Re: thousand separator formatting int, float, decimal values

    Mar 03, 2009 12:10 AM | LINK

    Can you use a IValueConverter on the column data binding instead of changing your data type?

    Mark

     

    Mark Salsbery
  • hazz

    hazz

    Member

    245 Points

    436 Posts

    Re: thousand separator formatting int, float, decimal values

    Mar 03, 2009 07:30 AM | LINK

    how? my imagination with IValueConverter's is still limited. thx for the idea though.

    hazz
  • meykih

    meykih

    Participant

    1049 Points

    260 Posts

    Re: thousand separator formatting int, float, decimal values

    Mar 03, 2009 10:12 AM | LINK

    First you need to create a class that includes interface IValueConverter. Something like this (if you have a TwoWayBinding you need to fill ConvertBack too):

        public class ThousandSeparator : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                decimal input = (decimal)value;

                string help = input.ToString();

                string output = input.ToString(); ;

                if (help.Length >= 4)
                {
                    output = help.Substring(0, help.Length-3) + "." + help.Substring(help.Length - 3, 3);
                }

                return output;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
     

    In your XAML you reference this class as a UserResource, give it a key (x:kex="myKey") and then call it where your binding is set: {Binding Path=xxx, Converter={StaticResource myKey}}

    Regards,
    Maike Ohlig

    Please mark post as answer if it helped you
  • hazz

    hazz

    Member

    245 Points

    436 Posts

    Re: thousand separator formatting int, float, decimal values

    Mar 03, 2009 12:31 PM | LINK

    thank you maike !  your substring works as follows but the System.Globalization String.Formats code below doesn't .....

    if (temp.Length >= 4) { output = temp.Substring(0, temp.Length - 3) + "," + temp.Substring(temp.Length - 3, 3);  }

     

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

    {   float input = (float)value;

        string temp = input.ToString();     string output;

        output = String.Format("{0:#,0}", temp);

    hazz