Home > Back-end >  How to Format float value from 11111111 to 1,111,111.00?
How to Format float value from 11111111 to 1,111,111.00?

Time:02-24

I've been all over the Internet, and since I have no much experience, I couldn't find a right solution.

As mentioned above, in my database I will have "big" values, where there are up to 9-10 digits, and I was requested to format them like, for example, instead of 23456789 -> 23,456,789.00, and show them like that in DataGrid.

This is how i populate DataGrid

probaDataContext proba = new probaDataContext();
        public MainWindow()
            {
                InitializeComponent();
    
                dgData.ItemsSource = proba.ProbaTabelas.ToList();
    }

And this is my XAML code

I found a way how to Format them to 23,456,789 , but I still need those two digits behind, with the dot.

<DataGrid Name="dgData" Margin="89,0,118,165" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Numbernumber" Binding="{Binding cifra, StringFormat=\{0:N0\}}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid

If the value is a whole number, they should be .00 , if a value has decimals like 23432.54, they should look like 23,432.54.

CodePudding user response:

String.Format("{0:n}", 1234);  // Output: 1,234.00

String.Format("{0:n0}", 9876); // No digits after the decimal point. Output: 9,876

so I suppose that changing this

StringFormat=\{0:N0\}

to this

StringFormat=\{0:N\}

is ok for you.

Note: Some cultures use , to mean decimal rather than . so be careful.

  • Related