當我像這樣分配整個值時遇到問題double myDouble = 1.0;...到一個變量。當我在 Telerik 的文本塊上顯示它時,它會顯示“1”而不是“1.0”。其他帶小數位的值按預期工作。編輯 我將 wpf 與 Telerik Datagrid 一起使用。但在將值放入 Datagrid 之前,我將其轉換為字符串。所以在此之前,我會像這樣分配值。這是我的課:public class MyClass{ public int Id {get;set;} public double MyDoubleVal {get;set;}}這就是我分配價值的方式var class = new MyClass { Id = 1, MyDoubleVal = 1.0 };當我調試并嘗試檢查該值時,它顯示為“1”而不是“1.0”?,F在,當我將要顯示的值放在 DataGrid 上時,我可以手動添加小數位,但如果有可以為我處理的格式化函數,我寧愿不這樣做。到目前為止,我已經嘗試了以下String.Format(""{0:00.0}", myClassInstance.MyDouble);myClassInstance.MyDouble.ToString("0.0")
3 回答

蝴蝶刀刀
TA貢獻1801條經驗 獲得超8個贊
嘗試在 XAML 中格式化您的雙精度值
<TextBlock Text="{Binding MyDoubleVal, StringFormat=n1}" />
如果只有 Content 屬性,則改用 ContentStringFormat

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
你有很多選擇。其中一些如下:
字符串.格式():
double a = 18.54657;//some number
Console.WriteLine(string.Format("{0:F2}", a);
// where F2 tells the function to format as a float with 2 decimals... for your usecase you can use F1
雙.ToString():
double a = 18.54657;//some number
Console.WriteLine(a.ToString("#.#");
//where the # after the period tells the function to include 1 number after the period(or 1 decimal point). You can add more # for more decimal points to show. As a side not you can do a.ToString("#,###.#") to add commas to numbers
- 3 回答
- 0 關注
- 254 瀏覽
添加回答
舉報
0/150
提交
取消