我試圖在創建條形圖時將可變整數資料轉換為百分比。但是,它似乎不起作用。我錯過了什么?
string x1 = var_Product_name;
int y1 = Tools.ConvertToInt_02(var_Amount);
// This is the code I used to convert the var_Amount to get the percentage and display as data series in bar graph together with the var_Amount value however this isn't working
int y2 = Tools.ConvertToInt_02(var_Amount)/ 100.ToString("0.00%");
BarChartData_Product_Records_Data_View.Add(new BarChartSourceData_Product_Records_Data_View(x1, y1, y2));
uj5u.com熱心網友回復:
最有可能var_Amount的范圍是 0..100,所以即使
100/100
結果為 1,而較小的值,例如
25/100
結果為 0,因為它y2是一個整數。為了修正它,改變y2的型別,支持十進制部分,例如float,double,或decimal。
uj5u.com熱心網友回復:
首先,如果Tools.ConvertToInt_02(var_Amount)回傳一個整數值,這一行將不會編譯:你有intvalue on before/和stringvalue after /。
你應該看到:
錯誤 CS0019:運算子“/”不能應用于“int”和“string”型別的運算元
如果假設var_Amount要格式化為百分比的雙精度值,請使用以下方法:
string specifier = "P";
var result = (var_Amount / 100).ToString(specifier, CultureInfo.CurrentCulture);
例如,如果var_Amount具有值12.3456的result將是12.35 %,當用于當前培養當前小數點字符.。
有關其他資訊,請參閱檔案:Double.ToString 方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/354091.html
標籤:C# asp.net-mvc
