我的 DataGrid 接收雙變數,我想將它們截斷到小數點后兩位或三位。我的xml代碼:
<DataGrid Name="McDataGrid" ItemsSource="{Binding}" Visibility="Collapsed" HorizontalAlignment="Center"
Margin="0, 200, 0, 0" VerticalAlignment="Top" CanUserAddRows="False" />
我所有涉及資料網格的 C# 代碼:
McDataGrid.DataContext = fillingDataGridUsingDataTable(string input).DefaultView;
McDataGrid.Visibility = Visibility.Visible;
fillDataGridUsingDataTable(input) 是一個函式,它回傳一個 DataTable,其中標題為字串,值為雙精度。
uj5u.com熱心網友回復:
您可以將ToString與格式說明符F與精度說明符(標準格式說明符)一起使用。無論如何都會將 double 值轉換為 a ,所以DataGrid會這樣做。使用接受 a 的多載使結果使用當前系統語言的正確小數分隔符。stringToStringIFormatProvider
以下示例string從double精度為 3 位小數的值創建數字:
double value = 1.23456;
var numericString = value.ToString("F3", CultureInfo.CurrentCulture); // "1.235"
要轉換double值,請使用Math.Round幫助器:
double value = 1.23456;
double roundValue = Math.Round(value, 3); // 1.235
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/458843.html
