Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Format float value from 11111111 to 1,111,111.00?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading